ColdFusion versus PHP – Uploading a File

I spent part of my vacation digging into PHP. I’d like to be much savvier with PHP because we’ve seen a big chunk of Flex adoption from the PHP community and I’d like to be able to talk to those developers more intelligently. Unfortunately my server-side programming chops are very, very rusty. I started out as a ColdFusion developer and I’ve been able to play with some of the new stuff in ColdFusion 9 but I haven’t built a real CF app in a long time. So I thought I’d do a bit of a comparison and I’ve started doing simple things in PHP and then replicating them in ColdFusion. The first one is file uploading.

Disclaimer: I’m assuming there are things I’m doing wrong. File uploading is fairly basic, but part of the reason I want to blog the basic stuff is so people can let me know where I’m messing up and what I could do better. So flame on in the comments.

The Code Comparison

PHP – file_upload.php

error_reporting(E_ALL | E_STRICT);
if( move_uploaded_file($_FILES['myfile']['tmp_name'], "{$_FILES['myfile']['name']}") )
print '<p>It verked!</p>';
} else {
print '<p>Problemo!</p>';
<form action="upload_file.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
<p><input type="file" name="myfile" /></p>
<p><input type="submit" name="submit" value="Upload" /></p>
<input type="hidden" name="submitted" value="true" />

ColdFusion – file_upload.cfm

<cfset currentPath = getCurrentTemplatePath()>
<cfset currentDirectory = getDirectoryFromPath(currentPath)>
<cffile action="upload" nameconflict="overwrite" filefield="form.myfile" destination="#currentDirectory#/coldfusion.gpx"/>
<cfif cffile.filewassaved eq true>
<p>It verked!</p>
<cfdump var="#cffile#"/>
<formaction="upload_file.cfm" enctype="multipart/form-data" method="POST">
<p><inputtype="file" name="myfile" /></p>
<p><inputtype="submit" name="submit" value="Upload" /></p>
<inputtype="hidden" name="submitted" value="true" />


I thought it was interesting how PHP and ColdFusion differ in the actual upload mechanism. Both of them just upload the file to a temporary location but then how the programmer deals with it is different. With PHP, you “move” the file to a location of your choice with the move_uploaded_file method. With ColdFusion you use cffile but call the upload attribute instead of the move attribute. That’s why you don’t put # signs around the form.myfile attribute because the upload function in cffile uses the POST info and that filefield attribute to to the moving/copying behind the scenes. Not sure which one I like better.

A few other bits. One, ColdFusion seems to give way, way more information about the file you’ve uploaded. I don’t know if there is a way to get that kind of detail from PHP, but with ColdFusion I get a bunch of attributes like filewassaved, fileexisted, filewasappended, serverfileext, serverfilename etc. Some of that I can parse from the PHP information, like the file name and file extension, but others, like if it was overwritten, or if the file already existed, don’t seem possible to get with PHP. Two, it is really, really annoying to use a relative file path with ColdFusion. The move_uploaded_file function uses a relative path so it’s easy to upload it to the same directory. With ColdFusion, it’s a pain which is why I had to set a couple of extra variables. Also, PHP seems to love to check that you’re below the max file size. You have to set one in your php.ini file and then send it again as a hidden form field. That seems completely redundant and I can’t figure out why they do that.

Like I said above, this is supposed to be a pretty basic example because I want to know if I’m doing something stupid. If so, or if you have any additional info on my thoughts above, let me know.

Show Comments

Hide Comments

Comments are closed.