如何在zend框架中命名上传的文件

San*_*kha 4 php zend-framework

我有一个表单包含和形式的代码就像这样的图像

$image->setDestination(APPLICATION_PATH . '/../public/images/upload'); 
Run Code Online (Sandbox Code Playgroud)

在我的控制器中我有

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

如何更改上传文件名.我想把它设置为

random(100).time().ext
Run Code Online (Sandbox Code Playgroud)

试试这段代码

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

}
Run Code Online (Sandbox Code Playgroud)

仍会在我的数据库中提供'backend/public/images/upload/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); 
Run Code Online (Sandbox Code Playgroud)

我正在使用Ubuntu

Oct*_*adu 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
));
Run Code Online (Sandbox Code Playgroud)

在你的控制器:

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