Zend Zend_File_Transfer_Adapter_Http重命名问题

时间:2008-12-19 13:10:49

标签: 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';
        }
    }
}

6 个答案:

答案 0 :(得分: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

答案 1 :(得分:6)

我设法通过设置过滤器来做到这一点。请注意,我没有设置目标路径。

$adapter= new Zend_File_Transfer_Adapter_Http();
$adapter->addFilter('Rename',array('target' => WWW_ROOT . '/photos/' . $this->memberId . '.jpg'));

$adapter->receive();

答案 2 :(得分:1)

根据文档,您不应将路径放在目的地

link text

答案 3 :(得分:1)

使用zend框架有一种更好,更安全的方式......

创建帮助程序类以检索文件扩展名。

class ImageUpload {

public function getExtension ($name)
{
    if($name){
    foreach ($name as $val){
        $fname=$val['name'];
      }
    $exts = split("[/\\.]", $fname) ;
    $n = count($exts)-1;
    $exts = $exts[$n];
    return $exts; 
    }

}

}

控制器中的

类ProfileController扩展了Zend_Controller_Action {     function indexAction()     {         $ this-> view-> title =“Profile”;         $ this-> view-> bodyCopy =“

请填写此表单。

”;

    $form = new ImgForm();

    if ($this->_request->isPost()) {
        $formData = $this->_request->getPost();
        if ($form->isValid($formData)) {
                $adapter = new Zend_File_Transfer_Adapter_Http();
                $adapter->setDestination('images/users/big');

                // getting extension

                $filename = $adapter->getFileInfo();
                $uhelper = new ImageUpload;  // cals for help to get file extension
                $extension = $uhelper->getExtension($filename); // got extension   


                 // success -- file handled

                 //rename
                 $auth = Zend_Auth::getInstance();
                 $identity = $auth->getIdentity();

                 $adapter->addFilter('Rename', array('target' => 'images/users/big/'.$identity->id.'.'.$extension,
                     'overwrite' => true));

                if (!$adapter->receive()) {
                 $form->addError($adapter->getMessages());

                }

        } else {
            $form->populate($formData);
        }
    }

    $this->view->form = $form;

}

当您的表单应该是:

    parent::__construct($options);
    $this->setName('upload');
    $this->setAttrib('enctype', 'multipart/form-data');


    $file = new Zend_Form_Element_File('file');
    $file->setLabel('File')
          ->addValidator('Count', false, 1)     // ensure only 1 file
          ->addValidator('Size', false, 102400) // limit to 100K
          ->addValidator('Extension' ,false, 'jpg,png,gif') // only JPEG, PNG, and GIFs

          ->setRequired(true);          


    $submit = new Zend_Form_Element_Submit('submit');
    $submit->setLabel('Upload');

    $this->addElements(array($file, $submit));

}

}

玩得开心

答案 4 :(得分:1)

public function getExtension($name){
    $names= explode(".", $name);
    return $names[count($names)-1];
}

答案 5 :(得分:0)

我必须拦截$ _FILES并在调用适配器之前进行更改

if(isset($_FILES['file1'])){
  $ext = pathinfo($_FILES['file1']['name']);
  $_FILES['file1']['name'] = 'image_'. $userid .'.'.$ext['extension'];
}
$adapter = new Zend_File_Transfer_Adapter_Http();

我确定有更好的方法,我不知道为什么过滤器不起作用我已经尝试了一切来使它工作。我有一个截止日期,所以上面的代码生产LOL

希望它有助于某人

埃里克