I have a form that contains and form of which code goes like this for image
$image->setDestination(APPLICATION_PATH . '/../public/images/upload');
and in my controller i have
if($countryForm->image->isUploaded())
{
$countryForm->image->receive();
$countryModel->addCountry(
$countryForm->getValue('name'),
$countryForm->getValue('description'),
$this->view->baseUrl('/images/upload/'.basename($countryForm->image->getFileName()))
);
}
how can i change the name of the filename uploaded. I want to set it to
random(100).time().ext
trying this code
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')
);
}
would still give 'backend/public/images/upload/picture.开发者_StackOverflowjpg' in my database
I have in my form the following code
$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);
and i am using Ubuntu
In your form class add the element like this:
$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
));
In you controller:
$originalFilename = pathinfo($form->image->getFileName());
$newName = rand(1,100) . time() . $originalFilename['extension'];
$form->cover->addFilter('Rename', $newName);
$data = $form->getValues();
Use addFilter()
http://framework.zend.com/manual/1.12/en/zend.file.transfer.filters.html#zend.file.transfer.filters.usage
If you include the filename (here you can use your rand() function) and add it to the target
array value it will rename it
From documentation:
$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));
Try:
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')
);
精彩评论