Zend Zend_File_Transfer_Adapter_Http 重命名问题 [英] Zend Zend_File_Transfer_Adapter_Http renaming question

查看:24
本文介绍了Zend Zend_File_Transfer_Adapter_Http 重命名问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在 Zend 中上传文件后重命名文件的问题.我不知道把重命名过滤器放在哪里.这就是我所拥有的.我试过移动东西,但我迷路了.目前它确实将文件上传到我的照片文件夹,但它没有重命名它.感谢您的帮助!

I've got a question about renaming a file after it's been uploaded in Zend. I don't know where to put the Rename Filter. Here's what I've got. I've tried moving things around, but I'm lost. Currently it does upload the file to my photos folder, but it doesn't rename it. Thanks for any help!

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 来重命名文件,也可以解析文件名以获取扩展名(如果您愿意).

Actually, there is an even easier way to do this. All you need to do is pass false as the second parameter for the getFileName method of the Zend_File_Transfer_Adapter_Http object. Then you can rename the file by appending a userID to it or parse the file name to get the extension out if you like as well.

// 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

这篇关于Zend Zend_File_Transfer_Adapter_Http 重命名问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆