gpt4 book ai didi

zend-framework - Zend_Form_Element_File 重命名文件保存扩展名

转载 作者:行者123 更新时间:2023-12-04 06:49:43 25 4
gpt4 key购买 nike

我使用 Zend Form 并上传文件。我需要为它重命名和用户 addFilter。但是,如果我尝试像代码中那样获取文件的扩展名,我会收到错误消息“文件太多,最多允许 '1',但给出了 '2'”。如果我尝试使用 $_FILES 获取扩展名,它看起来可以解决,但看起来很难看。你能告诉我如何重命名文件并保存它的扩展名吗?

        $form = new Form_ImportSubscribers();               
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
//it looks like it works but it's ugly solution
// $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
//causes an error "Too much files, maximum '1' are allowed but '2' are given"
$extension = pathinfo($form->file->getFileName(), PATHINFO_EXTENSION);
$form->file->addFilter('Rename', $accountId . '_' . time() . '.' . $extension);

if (!$form->file->receive()) {
$this->view->form = $form;
$this->view->listName = $list->list_name;
return;
}

最佳答案

如果 $form->file->getFileName() 返回多个值,我假设您上传多个文件。在这种情况下,您可以应用重命名过滤器并接收单个文件,如下所示:

/*@var $adapter Zend_File_Transfer_Adapter_Http */
$adapter = $form->file->getTransferAdapter();
$receivingOK = true;
foreach ($adapter->getFileInfo() as $file => $info) {
$extension = pathinfo($info['name'], PATHINFO_EXTENSION);
$adapter->addFilter('Rename', $accountId . '_' . time() . '.' . $extension, $file);
if (!$adapter->receive($file)) {
$receivingOK = false;
}
}

if (!$receivingOK) {
$this->view->form = $form;
$this->view->listName = $list->list_name;
return;
}

即使您不执行多个文件上传,它也应该可以工作。

关于zend-framework - Zend_Form_Element_File 重命名文件保存扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5231902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com