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.

Sunday, November 8, 2009

Flash caching PHP response

Recently I ran into an issue where I would call a PHP file that would generate a unique id and return that to Flash (I am sure I could do the same thing from within flash but decided to just use PHP's uniqid to solve this for now) The id that comes back is used for files the user is uploading. The problem I noticed is that Flash would make the call to the PHP file and then get back the same UUID everytime. I thought that perhaps Flash was caching the page and then just loading it instead of re-requesting it from the web server. In summary this was somewhat true. The page was being cached somewhere and needed some PHP header references to prevent from caching properly.


Problem:
Flash kept loading the same values from a PHP script that should have been generating different values on every call.

Solution:
I included these header functions and parameters at the top of my PHP file.
header( "Pragma: no-cache" );

header( "Cache-Control: no-cache" );

header( "Expires: Sat, 01 Jan 2000 00:00:00 GMT" );

 Happy programming.

Getting file extension with ActionScript

It has been some time since I have posted, but I figured this one quick function for extracting a file extension was worth a quick post.

Problem:
You are dealing with files and want a quick way to extract the files extension.

Solution:
A simple function that will return back you to the extension of any file with or without a full path reference preceding it is this:
function getFileExtention(str:String):String{
    return str.substring(str.lastIndexOf(".") + 1, str.length);
}
NOTE: This will not account for URLs that have GET parameters on the end of them. To do this, Google search getting file extension using regexp or regular expressions.

 I hope this helps someone who is looking for a quick solution online. Good luck.

Sunday, August 2, 2009

Symfony and XAMPP httpd.conf file issues

Here is another short simple post of something I had to figure out and could not find documented anywhere.

Problem: When install the Symfony MVC on my localhost (Windows Vista), I would copy-paste the additional httpd.conf settings to the httpd.conf file in the xampp folder. When I would restart Xampp (or apache more specifically) I would get a windows error telling me that Apache had failed to start properly.

Solution: Although I had switched the paths to be specific to windows, I did not know until reading documentation later that the apache still requires all backslashes to be forward slashes in the file paths.

Simple mistakes, but now solved.

Monday, July 20, 2009

build.xml does not exist! - Apache Ant

Problem: When trying to run a command with ant after installation, you might see this error:
Buildfile: build.xml does not exist!
Build failed

Solution/Explanation: This message is showing you that ant works and that it is looking for your project file. The documentation by Apache gives this explanation:
You can check the basic installation with opening a new shell and typing ant. You should get a message like this
Buildfile: build.xml does not exist!
Build failed
So Ant works. This message is there because you need to write an individual build file for your project. With a ant -version you should get an output like
Apache Ant version 1.7.0 compiled on December 13 2006
You can see that this is not really an error but just a notification.

Friday, July 10, 2009

Linux Permission Made Easy

When learning Linux, sometimes people have a tough time understanding how folder/file/application permissions work. I am writing this to show you a quick easy way to figure out which permissions to use.

Problem: 
Folder/file permissions on Linux can be difficult to understand. Sometimes you will see 0775 or 735 or if you are looking at files in Linux (using ll or similar command) you will see something like -rw-rw-r--.

Solution: Figuring out what these mean is easy, you just need to perform two steps and you can have permissions the way you want in no time. Also, keep in mind that the 0775 is basically the same as 735, the leading zero on the second has been removed and is not displayed in this report. For the purposes of this post, we are going to ignore the leading zero. Let's look real quick how 0775 (or 735) and the display of "-rw-rw-r--" relate.

Understanding how we got the 7
The second number of 0775 is actually made of from this simple chart by adding which permissions you want enabled:
4 : Read
2 : Write
1 : Execute
0 : None

These four options are compined in various ways to give you exactly the permission you want for each digit in the full sequence of 0775. The second number has all the permissions ( 4+ 2 + 1 = 7) meaning it can be read, written, and executed (if executable). Like wise, setting this to read and write only, would equal the number 6.

So, now understand the full sequence as it applied to the file/folder/application
The numbers representing the our demo sequence of 0775 have been charted to show what each one represents.
0 : Reserved (we not discussed this in this tutorial)
7 : User (the permissions of the user)
7 : Group (groups made in Linux, this could be staff, etc.)
5 : World (the permissions the rest of the world [or in our case, operating system users/guests])

Now let's look at how 0775 and -rw-rw-r-- relate
You may have already guessed it, but here it goes. You will notice there are 3 main permissions (read, write, execute). The first letter in the sequence -rw-rw-r-- is that one that we don't talk about in this tutorial, the leading zero in 0775. The next three letters or hyphens (rw-), make up the read, write, and execute permissions for the user group. The following 3 after that make up the permissions for the Group. And the last 3 represent the world permissions.

Now you hopefully have an idea of how to read, understand, and how to set up your own permissions.

Disclaimer: At the time of writing this, I am new to some things Linux and am bound to get things wrong. If I have listed something incorrectly, please let me know so I can change it and provide good reliable information. Thanks.