My implementation was in fact using sfValidatorFile to validate the file's mime type, etc. Trusting this to be as the documentation said it is, I looked for every other possible reason why my files wouldn't upload.sfValidatorFile
is quite interesting as it does a number of things:
- Validates that the uploaded file is an image in a web format (
mime_types
)- Renames the file to something unique
- Stores the file in the given
path
- Updates the
logo
column with the generated name
Problem:
Files wouldn't upload properly in Symfony 1.4 even with using the sfValidatorFile.
Solution:
Manually loop over and move files to the upload directory.
The code I used in my actions file was like this:
I hope this helps someone figure out their code issues.
if ($this->form->isValid())
{
foreach ($request->getFiles($this->form->getName()) as $uploadedFile)
{
$uploadDir = sfConfig::get('sf_upload_dir') . '/assets';
move_uploaded_file($uploadedFile["tmp_name"], $uploadDir . "/" . $uploadedFile["name"] );
}
$this->redirect('media/thumbnailSelector?'.http_build_query($this->form->getValues()));
}
Happy coding.
3 comments:
You are my hero of the day, thanks!
Hi,
I looked at this as well, with version version: 1.4.11
I was able to use the file validator in the admin generated backend.
In the doSave method of the generated formclass I had
$file = $this->getValue('image'); //tbs - change to image1, image2...
if ( $file )
{
$filename = sha1($file->getOriginalName().microtime().rand()).$file->getExtension($file->getOriginalExtension());
$savedName = $file->save($uploadDestPath . $filename);
$newSavedName = $file->setSavedName( $filename);
Where file is a sfValidatedFile object.
the $file->save call works, however it saved the value to the database with the entire file system path.
I added a call setSavedName to just set it as the name I want.
@Daniel yeah, he gets the hero award from me too. This just saved me tons of time.
Post a Comment