/ / zendフレームワークでアップロードされたファイルの名前を付ける方法 - php、zend-framework

zendフレームワークでアップロードされたファイルの名前を付ける方法 - php、zend-framework

私は、イメージのためにどのコードがこのようになっているかを含むフォームを持っています

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

}

私のデータベースにはまだ "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);

私はUbuntuを使用しています

回答:

回答№1の場合は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:pictureuploads");

// Set a new destination path and overwrites existing files
$upload->addFilter("Rename", array("target" => "C:pictureuploads", "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")
);

回答№2のための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();