代码之家  ›  专栏  ›  技术社区  ›  Corey

zend zend_file_transfer_adapter_http重命名问题

  •  5
  • Corey  · 技术社区  · 16 年前

    我有一个关于在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 回复  |  直到 16 年前
        1
  •  9
  •   electromute    15 年前

    实际上,有一种更简单的方法可以做到这一点。您只需将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
    
        2
  •  6
  •   ScArcher2    16 年前

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

    $adapter= new Zend_File_Transfer_Adapter_Http();
    $adapter->addFilter('Rename',array('target' => WWW_ROOT . '/photos/' . $this->memberId . '.jpg'));
    
    $adapter->receive();
    
        3
  •  1
  •   stunti    16 年前

    根据文档,您不应该将路径放在目标中

    link text

        4
  •  1
  •   genetrix    15 年前

    Zend框架有更好更安全的方法…

    创建助手类以检索文件扩展名。.

    类图像上载{

    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_操作 { 函数indexaction() { $this->查看->title=“个人资料”; $this->查看->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));
    
    }
    

    }

    玩得高兴

        5
  •  1
  •   magnai    14 年前
    public function getExtension($name){
        $names= explode(".", $name);
        return $names[count($names)-1];
    }
    
        6
  •  0
  •   Eric    16 年前

    在调用适配器之前,我必须截取$\u文件并进行更改。

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

    我相信有更好的方法,我不知道为什么过滤器不工作,我已经尽力让它工作。我有一个最后期限,所以上面的代码变成了LOL

    希望它能帮助别人

    埃里克