/ / ako pomenovať nahraný súbor v zend framework - php, zend-framework

ako pomenovať nahraný súbor v rámci zend - php, zend-framework

Mám formulár, ktorý obsahuje a formu, ktorý kód ide takto pre obrázok

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

a v mojom kontrolórovi mám

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

ako môžem zmeniť názov nahraného súboru. Chcem to nastaviť

random(100).time().ext

skúšať tento kód

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

}

by stále dali "backend / public / images / upload / picture.jpg" do mojej databázy

Mám vo svojom tvare nasledujúci kód

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

a používam Ubuntu

odpovede:

3 pre odpoveď č. 1

Použiť addFilter ()

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

Ak zadáte názov súboru (tu môžete použiť funkciu rand ()) a pridať ju do priečinka target hodnota poľa ju premenuje

Z dokumentácie:

$upload = new Zend_File_Transfer();

// Set a new destination path
$upload->addFilter("Rename", "C:pictureuploads");

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

skúste:

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

5 pre odpoveď № 2

Vo svojej triede formulárov pridajte tento prvok:

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

Vo vás ovládač:

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