2010-12-05 73 views
4

我有一个包含其中的代码是这样的图像如何命名Zend框架中上传的文件

$image->setDestination(APPLICATION_PATH . '/../public/images/upload'); 

,并在我的控制器我有一个表格和表格

if($countryForm->image->isUploaded()) 
{ 
    $countryForm->image->receive();  
    $countryModel->addCountry(
    $countryForm->getValue('name'), 
    $countryForm->getValue('description'), 
    $this->view->baseUrl('/images/upload/'.basename($countryForm->image->getFileName())) 
); 
} 

怎么能我更改上传的文件名称。我想将它设置为

random(100).time().ext 

尝试这种代码

if($form->image->isUploaded()){ 
    $upload = new Zend_File_Transfer(); 
    $upload->addFilter('Rename', array('target' => APPLICATION_PATH.'/../images/upload/'.time().'.jpg', 'overwrite' => true)); 
    $form->image->receive(); 
    $filename = $form->image->getFilename(); 
    $pageModel->addPage($form->getValue('pagetitle'), 
    $form->getValue('pagemetakeyword'), 
    $form->getValue('pagemetadescription'), 
    $form->getValue('pagecategory'), 
    $filename, 
    $form->getValue('pagecontent'), 
    $form->getValue('pagestatus') 
); 

} 

仍然给予“后端/公/图像/上传/ picture.jpg”在我的数据库

我有我形成下面的代码

$image = $this->createElement('file', 'image'); 
$image->setLabel('Image: '); 
$image->setRequired(FALSE); 
$image->setDestination(APPLICATION_PATH . '/../public/images/upload/'); 
$image->addValidator('Count', false, 1); 
$image->addValidator('Size', false, 1024000); 
$image->addValidator('Extension', false, 'jpg,jpeg,png,gif'); 
$this->addElement($image); 

,我使用Ubuntu

回答

3

使用addFilter()方法

http://framework.zend.com/manual/1.12/en/zend.file.transfer.filters.html#zend.file.transfer.filters.usage

如果包括文件名(在这里你可以用你的rand()函数),并把它添加到target数组值将其重命名为

从技术文档:

$upload = new Zend_File_Transfer(); 

// Set a new destination path 
$upload->addFilter('Rename', 'C:\picture\uploads'); 

// Set a new destination path and overwrites existing files 
$upload->addFilter('Rename', array('target' => 'C:\picture\uploads', 'overwrite' => true)); 

尝试:

if($form->image->isUploaded()){ 
     $upload = new Zend_File_Transfer(); 
     $files = $upload->getFileInfo(); 
     //You should also add file validation, see: 
//http://framework.zend.com/manual/en/zend.file.transfer.introduction.html#zend.file.transfer.introduction.checking 
     $upload->addFilter('Rename', APPLICATION_PATH.'/../images/upload/'.time().'.jpg', $files[0]); 
     $upload->receive($files[0]); 
     $filename = $files[0]->getFilename(); 
     $pageModel->addPage($form->getValue('pagetitle'), 
     $form->getValue('pagemetakeyword'), 
     $form->getValue('pagemetadescription'), 
     $form->getValue('pagecategory'), 
     $filename, 
     $form->getValue('pagecontent'), 
     $form->getValue('pagestatus') 
    ); 
+0

哪里做?在形式?? – 2010-12-05 10:17:02

+0

isUploaded()if,before receive() – Ashley 2010-12-05 10:34:12

5

在窗体类中添加这样的元素:

$fileDestination = realpath(APPLICATION_PATH.'/../images/upload/'); 
$this->addElement('file','cover', array(
    'label'  => 'Cover:', 
    'required' => false, 
    'destination' => $fileDestination, 
    'validators' => array(
     array('Count', false, array(1)), 
     array('Size', false, array(1048576 * 5)), 
     array('Extension', false, array('jpg,png,gif')), 
    ), 
    'decorators' => $this->_elementDecorator 
)); 

在你的控制器:

$originalFilename = pathinfo($form->image->getFileName()); 
$newName = rand(1,100) . time() . $originalFilename['extension']; 
$form->cover->addFilter('Rename', $newName); 
$data = $form->getValues();