Cor*_*rey 5 zend-framework zend-file
我有一个关于在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';
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
实际上,有一种更简单的方法可以做到这一点.您需要做的就是将false作为Zend_File_Transfer_Adapter_Http对象的getFileName方法的第二个参数传递.然后,您可以通过向其附加userID来重命名该文件,或者解析文件名以获得扩展名(如果您也喜欢).
// 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
Run Code Online (Sandbox Code Playgroud)
我设法通过设置过滤器来做到这一点.请注意,我没有设置目标路径.
$adapter= new Zend_File_Transfer_Adapter_Http();
$adapter->addFilter('Rename',array('target' => WWW_ROOT . '/photos/' . $this->memberId . '.jpg'));
$adapter->receive();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22644 次 |
| 最近记录: |