我有一个关于在 Zend 中上传文件后重命名文件的问题。我不知道将重命名过滤器放在哪里。这就是我所拥有的。我尝试过移动东西,但我迷路了。目前,它确实将文件上传到我的照片文件夹,但没有重命名。谢谢你的帮助!
if($this->_request->isPost())
{
$formData = $this->_request->getPost();
if ($form->isValid($formData))
{
$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->setDestination(WWW_ROOT . '/photos');
$photo = $adapter->getFileInfo('Photo');
$adapter->addFilter('Rename', array(
$photo['Photo']['tmp_name'],
WWW_ROOT . '/photos/' . $this->memberId . '.jpg',
true
));
if ($adapter->receive())
{
echo 'renamed';
}
}
}
实际上,还有一种更简单的方法可以做到这一点。您需要做的就是将 false 作为 Zend_File_Transfer_Adapter_Http 对象的 getFileName 方法的第二个参数传递。然后,您可以通过附加用户 ID 来重命名该文件,或者如果您愿意,也可以解析文件名以获取扩展名。
// upload a file called myimage.jpg from the formfield named "image".
$uploaded_file = new Zend_File_Transfer_Adapter_Http();
$uploaded_file->setDestination('/your/path/');
try {
// upload the file
$uploaded_file->receive();
} catch (Zend_File_Transfer_Exception $e) {
$e->getMessage();
}
$file_name = $uploaded_file->getFileName('image', false);
// this outputs "myimage.jpg"
$file_path = $uploaded_file->getFileName('image');
// this outputs "/your/path/myimage.jpg"
// now use the above information to rename the file
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)