2

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.

4 Answers 4

6

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']);
}
Sign up to request clarification or add additional context in comments.

2 Comments

of course, that's the "regular" php way to do it, it will work regardless of the framework used
Yeah, I suppose that is what I was looking for if I don't have a form class to use.
3

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!

3 Comments

Thanks for the link to the documentation
Check that link, it seems to be broken
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...)
1

Thank you friend. I'm looking for this code for 2 days. I'm new to php even i'm trying symfony. My code for symfony.

$this->filetype = $_FILES['file']['type'];
foreach ( $request -> getFiles () as  $file )
{ 
    if(move_uploaded_file ( $file [ "tmp_name" ],  sfConfig::get('sf_upload_dir').'/myfile/'.  $file [ 'name' ]))
    { 
        $this-setTemplate('success');
    } 
    else
    {
        $this-setTemplate('failure')
    }
} 

same way use the $filetype to check the uploaded file type. Check php file reference. For file types. Enjoy..

Comments

1

If you're still looking why you can't access to the uploaded file using $request->getFiles(), it might be because you forgot to add enctype="multipart/form-data".

Actually, you should have a form like this:

<form enctype="multipart/form-data" action="/yourdestination" method="post">
   <input type="file" name="somedummyfile"/>
   <input type="submit" value="Send"/>
</form>

And your action should do something like this:

$dummyfile = $request->getFiles('somedummyfile');
echo $dummyfile['tmp_name']; // Current filepath
echo $dummyfile['name']; // filename

Hope it helps !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.