2010-12-05 3 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') 
); 

} 

будет по-прежнему давать 'бэкэнд/Public/изображения/загрузки/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)); 

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

где сделать это ?? в виде?? –

+0

после isUploaded() if, before receive() – Ashley

+0

и массив ('target' => 'C: \ picture \ uploads') ?? могу ли я добавить имя файла как массив ('target' => 'C: \ picture \ uploads'. $ filename) ?? –

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(); 
+0

Нужно добавить дополнительную точку между именем и расширением. $ newName = rand (1,100). time(). '.'. $ OriginalFilename [ 'расширение']; –

Смежные вопросы