1. Home
  2. Computing & Technology
  3. PHP / MySQL
photo of Angela Bradley

Angela's PHP / MySQL Blog

By Angela Bradley, About.com Guide to PHP / MySQL

Uploading Files with PHP

Tuesday May 23, 2006
You're in a hurry to show a friend a photo you just took, and instead of opening up your FTP program and uploading it you just whip over to ImageShack and upload it there.

You can actually upload files from your browser to your server like this using a very small PHP script. The files are then saved to whatever folder you specify on your server. Remember if you plan to utilize this and give access to someone other than yourself, make sure you write in some security measures to keep people from uploading malicious code.

Comments

September 16, 2007 at 11:28 pm
(1) cibertrix says:

I am using your script exactly as is - one HTML file and one PHP file, the folder has been created for the php, etc. but I just cannot get this to work - has the script been updated or anything?

September 26, 2007 at 12:45 am
(2) Anon says:

bad article. (!($filetype == “image/gif”)) ??? why not just $filetype != “image/gif” also, that is a very bad, yes BAD way to check if a file is of a certain type. imagine a rigged http request. oh yes it can be done very easily

September 27, 2007 at 4:07 pm
(3) gsitts says:

The PHP line reporting back that the file uploaded… “The file xxxx has been uploaded” has a small error that prevents it from reporting the file name. the line:

echo “The file “.basename( $_FILES[’uploadedfile‘][’name’]). ” has been uploaded”;

should be:

echo “The file “.basename( $_FILES[’uploaded‘][’name’]). ” has been uploaded”;

to correctly report the file name.

November 15, 2007 at 9:20 am
(4) SAM says:

I WANT TO UPLOAD A PIC TO A WEB PAGE AND TRIED THIS PHP BUT I MUST HAVE DONE SOMETHING WRONG. CAN ANYONE HELP??
SLIMSAM46@YAHOO.COM

December 13, 2007 at 9:02 pm
(5) justin says:

I can not get this code to work, i COPY AND PASTED EVERYTHING and created a /upload folder on the ftp in the correct directory and it gives me:
Sorry, there was a problem uploading your file.

please help!
jdbeitz@gmail.com

December 14, 2007 at 8:27 am
(6) php says:

Did you CHMOD the directory correctly? That is an important step in the script.

February 1, 2008 at 3:37 am
(7) cda says:

i’ve been try to CHMOD the directory.. but still can not work… why?
pls help

February 15, 2008 at 11:15 pm
(8) dana says:

The CHMOD worked for me! However the upload will not allow one file over 3 MB to upload. What is causing this file limitation?

Any ideas will be great!
dana.mathews@comcast.net

February 20, 2008 at 10:18 pm
(9) Jared says:

Not sure what is going on. The Global_register variable on the server is set to off so I need to get the $_FILES information from a $_POST apparently. I can’t seem to get it to work properly.

April 3, 2008 at 12:41 pm
(10) William, Los Angeles, CA says:

I’m confused… $uploaded_size and $uploaded_type are never set. They of course evaluate out to nothing at all and that part of the code doesn’t do anything. The $_FILES[’uploaded’][type] and $_FILES[’uploaded’][’size’] do contain this information.

June 16, 2008 at 4:32 am
(11) shahid islam says:

i understood the whole code given here but i can’t find any sample i.e example.

August 7, 2008 at 5:22 pm
(12) 9ale7 says:

hi, it worked but need improvments, first i think people need to change cmhd, (i didn’t change it i simly used another website, because some wont allow any way), the other thing as (Anon) saied in the posts, there should be only a single
=
not ( not two of them)
==
but they wont change anything !! because you change all of the variables and still be able to upload .php or (if you changed the size) upload more that you limited ?!

also all the these messages:

Your file is too large.
No PHP files
The file has been uploaded

what is left !!!!!! heheheh :)

also ( not a problem but a request ) there sould be a message saying :
“you will be redirected to the main page after few seconds. ”
because the only way to leave the page is to hit the Back button.

please if you found a way to fix the problems, and to say the specific resones for the problems,
please email me at :

riffaey@w.cn

August 7, 2008 at 5:27 pm
(13) 9ale7 says:

in the previouse comment at the end i ment that someone should mention the html code that going to redirect the visitor back PLUS saying that it will.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL

©2008 About.com, a part of The New York Times Company.

All rights reserved.