5

我有一个关于在 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';
        }
    }
}
4

6 回答 6

9

实际上,有一种更简单的方法可以做到这一点。您需要做的就是传递 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
于 2009-10-28T23:25:09.380 回答
6

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

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

$adapter->receive();
于 2009-02-18T09:49:18.453 回答
1

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

链接文本

于 2009-01-16T10:35:23.750 回答
1

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_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));

}

}

玩得开心

于 2009-03-24T16:07:24.440 回答
1
public function getExtension($name){
    $names= explode(".", $name);
    return $names[count($names)-1];
}
于 2010-04-23T05:55:46.487 回答
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

希望它可以帮助某人

埃里克

于 2009-02-03T23:21:44.633 回答