在zend中重命名文件后上传文件

时间:2011-06-22 06:01:04

标签: file-upload zend-form file-rename

这是我的表格

class Admin_Form_RoomtypeForm extends Zend_Form
{
    public function init()
    {

       $name = new Zend_Form_Element_Text('name');
       $name->setLabel('Name :');

       $imagePath = '/home/dinuka/image';

       $image = new Zend_Form_Element_File('image');
       $image->setLabel('Image URL :');
       $image->setDestination($imagePath);
       $image->addValidators(array(array('IsImage', false)));

       $submit = new Zend_Form_Element_Submit('submit');

       $this->addElements(array($name, $image, $submit));
   }
}

这是我的控制器

class Admin_RoomtypesController extends Zend_Controller_Action 
{
    public function addAction()
    {
         $form = new Admin_Form_RoomtypeForm();

         if ($this->getRequest()->isPost()) {
              $formData = $this->getRequest()->getPost();
              $form->populate($formData);       

              $name = $form->getValue('name');

         }
    }
}

现在我想在更改文件名后将文件上传为$ name值。我该怎么办?

2 个答案:

答案 0 :(得分:0)

我必须为我的一个项目做这样的事情,除了我需要唯一的文件名。这不是一个完美的解决方案,但它可能会让你走上正轨:

<?php
class My_Filter_UniqueFilename implements Zend_Filter_Interface
{
  public function filter($value)
  {
    $upload_dir = pathinfo($value, PATHINFO_DIRNAME);
    $ext = pathinfo($value, PATHINFO_EXTENSION);
    $filename = $upload_dir . "/" . md5(microtime()) . "." . $ext;
    $result = rename($value, $filename);

    if($result === true) {
    return $upload_dir . $filename;
    }

    require_once 'Zend/Filter/Exception.php';
    throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. An error occured while processing the file.", $value));
  }
}

<?php class My_Filter_UniqueFilename implements Zend_Filter_Interface { public function filter($value) { $upload_dir = pathinfo($value, PATHINFO_DIRNAME); $ext = pathinfo($value, PATHINFO_EXTENSION); $filename = $upload_dir . "/" . md5(microtime()) . "." . $ext; $result = rename($value, $filename); if($result === true) { return $upload_dir . $filename; } require_once 'Zend/Filter/Exception.php'; throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. An error occured while processing the file.", $value)); } }

答案 1 :(得分:0)

最后我完成了以下操作。

-Form -

class Admin_Form_RoomtypeForm extends Zend_Form
{
    public function init()
    {

       $name = new Zend_Form_Element_Text('name');
       $name->setLabel('Name :');

       $image = new Zend_Form_Element_File('image');
       $image->setLabel('Image URL :');           
       $image->addValidators(array(array('IsImage', false)));

       $submit = new Zend_Form_Element_Submit('submit');

       $this->addElements(array($name, $image, $submit));
   }
}

-Controller -

class Admin_RoomtypesController extends Zend_Controller_Action 
{
    public function addAction()
    {
         $form = new Admin_Form_RoomtypeForm();

         if ($this->getRequest()->isPost()) {
              $formData = $this->getRequest()->getPost();
              $form->populate($formData);       

              $name = $form->getValue('name');
              $image = $form->getValue('image');

              $temp = explode(".", $image);
              $ext = $temp[count($temp)-1];

              $imageName = $name .'.'. $ext;

              $fullPath  = $imagePath ."/". $imageName; 
              copy("/tmp/". $image, $fullPath);    
         }
    }
}

默认图像上传到/ tmp文件夹。我们可以使用setDestination()方法更改它作为我的示例。

感谢所有