Laurent Melmoux a écrit :
Thomas,
my comment :
Thomas Weidner a écrit :
Hy,
I was thinking of 2 naming strategies :
1) You are storing the name of the file in the db
here the current solution is perfect, you get the file name with
getValue() ready to be saved in the db
2) You are NOT storing the name of the file in the db and named it
base on the primary key value
lets say you have this naming scheme : pkValue__photo (1__photo1,
1__photo2, ..., 56__photo1, 56__photo2)
so you call getValue() have the file stored
insert the data in the db, fetch the pk value with lastInsertedId()
and then reapply the Rename filter
There is one big difference which is the problem in your assumption.
getValue does NOT return the filename it receives the file and makes
several other data in background. The filename is not the value of
the file element.
Looking at Zend_Form_Element_File::getValue() we get the new filename
after filter are apply. Am I wrong ?
if (!$this->receive()) {
return null;
}
$filename = basename($this->getFileName());
$this->_value = $filename;
return $filename;
So the logical way in my opinion would be to call getFileName, change
the name according to the primary key of the database by applying the
rename filter, and then, as last step calling getValues().
The problem here is that I have to call getValues() first to have the
data save in the db and get the last Inserted Id
If validation or anything other fails, revert db entry.
You could even validate before attaching the rename filter because
validation does not receive the file.
if ($file->isValid()) {
$file->getFileName();
get id, rework filename
$file->addFilter('rename', $newfilename);
->getValues();
}
I have tried something like below after a call to getValues() and
getting the last Inserted Id $uid
It is working all find except when my file is not require and it is send
with the form.
I don't know where to check if the file have been send with the form or
not. isReceived() return true in both case photo (file attach or not to
the form). May be there is a need for an other flag like
$this->_files[$file]['submited'] ?
$sourceName = $this->_form->getElement('photo')->getFileName();
require_once 'Zend/Filter/File/Rename.php';
$options = array(
'source' => $sourceName ,
'target' => $_SERVER['DOCUMENT_ROOT'] . 'medias/news/photos/' .
$uid . '__photo' ,
'overwrite' => true);
$renameFileFilter = new Zend_Filter_File_Rename($options);
$renameFileFilter->filter($sourceName);