views:

863

answers:

3

I haven't done much file uploading in Symfony to this point and I'm trying to figure out how to do it in 1.2+

I can get the files submitted to the action and retrieve them via:

$files = $request->getFiles();

How can I get then save the file to the file system? I don't see any documentation besides the old 1.0 depreciated code.

It looks like this has been moved somehow into the form system but I don't have a specific for for this one-off page that I have that does nothing but upload a few files.

How can I save these files to disc.

+2  A: 

Inside the action, after you bind the params to the form:

  $file = $your_form->getValue('your_file_field');

  $filename  = $file->getOriginalName(); //or whatever name
  $extension = $file->getExtension($file->getOriginalExtension());
  $file->save(sfConfig::get('sf_upload_dir').'/'.$filename.$extension);

btw, you can find the documentation for Symfony 1.2 in the corresponding Symfony Forms in Action book: Chapter 02 file upload

ps : Don't forget to manually add the enctype="multipart/form-data" to your html form!

danii
Thanks for the link to the documentation
Failpunk
Check that link, it seems to be broken
Failpunk
fixed link, there seems to be a problem with the symfony page and external links (it doesn't like the 'www' part in the url...)
danii
A: 

If you are not looking to upload the files using the symfony forms then the following coded will also work:

foreach($request->getFiles() as $file)
{
    move_uploaded_file($file["tmp_name"], $directory . "/" . $file['name']);
}
Failpunk
of course, that's the "regular" php way to do it, it will work regardless of the framework used
danii
Yeah, I suppose that is what I was looking for if I don't have a form class to use.
Failpunk