Monday, January 25, 2010

Uploading a File in Symfony 1.4

Being new with Symfony, I decided to learn on their most recent stable version (1.4). For my work, I needed to be able to upload a file from the user when they submitted a form. While reviewing the Jobeet tutorial they have posted to help users learn Symfony, I found this:
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
 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.

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:

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()));
        }
I hope this helps someone figure out their code issues.

Happy coding.

Wednesday, January 13, 2010

Symfony XAMPP windows white screen or blank page issue

Recently while installing Symfony on my localhost using the xampp package, I realized I was having issues once I was trying to get the screen to load. When I typed in the url using localhost:8080, I was greeted by a white page with no source at all. Luckily the solution was very simple.

Problem:
While following the steps for setting up Symfony, finally testing it I was greeted with a blank page.

Solution:
When I checked the frontend_dev.php page, it told me I had an error with my doctrine plugin setup. So I went back to the page where the manual discussed that configuration and repeated those steps verifying in Windows Explorer that thing were happening for each command. This solved my problem and the page showed up just fine.

If you are not sure why your page isn't loading, first check the frontend_dev.php page, that will most likely explain the error. Raw exceptions are obstructed from appearing on production as a security feature in Symfony.

Best of luck programming.