1. Computing
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.

September 24, 2008 at 5:21 pm
(14) Peter says:

The script works, but it doesn’t actually upload anything. As far as I can see I have the correct path, etc, and the script even says my file has been uploaded. But when I check, there’s no file there. Is anyone else getting that?

Also, the redirect problem after upload is finished can be solved by including a simpe rediret like this:

echo “The file “. basename( $_FILES

['uploaded']['name']). ” has been uploaded. Returning you to the uploads page.”;

September 24, 2008 at 10:43 pm
(15) mark says:

i made a folder called ‘upload’ and called both html and php files ‘upload’ and i wanted people to upload just text files so i changed the code (after hours of trying, i’m a newbie) to this-
350000)
{
echo “Your file is too large.”;
$ok=0;
}

//This is our limit file type condition
if (isset($uploaded_type) && $uploaded_type ==”text/php”)
{
echo “No PHP files”;
$ok=0;
}
if (isset($uploaded_type) && $uploaded_type ==”text/cgi”)
{
echo “Not an approved file type.”;
$ok=0;
}
if (isset($uploaded_type) && $uploaded_type ==”text/html”)
{
echo “Not an approved file type.”;
$ok=0;
}
if (isset($uploaded_type) && $uploaded_type ==”text/asp”)
{
echo “Not an approved file type.”;
$ok=0;
}
if (isset($uploaded_type) && $uploaded_type ==”text/pl”)
{
echo “Not an approved file type.”;
$ok=0;
}
if (isset($uploaded_type) && $uploaded_type ==”text/gif”)
{
echo “Not an approved file type.”;
$ok=0;
}
if (isset($uploaded_type) && $uploaded_type ==”text/jpg”)
{
echo “Not an approved file type.”;
$ok=0;
}
if (isset($uploaded_type) && $uploaded_type ==”text/png”)
{
echo “Not an approved file type.”;
$ok=0;
}
if (isset($uploaded_type) && $uploaded_type ==”text/tif”)
{
echo “Not an approved file type.”;
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo “Sorry your file was not uploaded”;
}

//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo “The file “. basename( $_FILES['uploaded']['name']). ” has been uploaded”;
}
else
{
echo “Sorry, there was a problem uploading your file.”;
}
}
?>

it works, if you just subtract the file types you want to get through should be okay..can i ask you a quick question? where abouts in the code do i include the redirect code you helpfully posted?

September 24, 2008 at 10:46 pm
(16) Mark says:

the top bit of code got cut off…

350000)
{
echo “Your file is too large.”;
$ok=0;

October 21, 2008 at 8:48 am
(17) Naleglamype says:
January 18, 2009 at 10:26 pm
(18) pphhpp says:

For all who are struggling with this, some comments that may help. You have to set the permissions of the destination folder, on your webserver (hosting service), to allow writing by “anyone”. How you do that varies from hosting service to hosting service. Chmod 0777 may work, if you can execute it. On my service, I had to select the folder and click “change permissions”, then check the right boxes.
Note again, this is for the FOLDER that the file is going to be stored in.

If everything else seems right, it’s probably a permissions problem.

Good luck all.

January 18, 2009 at 10:29 pm
(19) pphhpp says:

Another point — there is a global environment variable somewhere that limits uploads to 3 Gig. I remember reading about it, but I don’t know where it is or what it’s called. Sorry I can’t help more.

April 13, 2009 at 8:42 am
(20) Chupo_cro says:

@pphhpp:
No, the destination folder doesn’t have to have the permission 777, use 755 instead.

April 23, 2009 at 7:39 am
(21) Krestort says:

Recently one of my friends started an obsession with the actor Nicholas Cage (mostly because their names are both Nicholas – sounds strange but he is strange and that isn’t the point). After asking around the rest of my friends he seems to be a very controversial figure.
What does the forum think? do you love the all action superhero? Or do you hate the droning voice of the man who does nothing but action shooters?

July 10, 2009 at 9:32 pm
(22) Bert Onstott says:

I guess this is probably a series of dumb questions, but here goes anyway.

1. Where do you put upload.php? Is that in a separate file from the web page that has the form?
2. When you build a form with method=”POST” enctype=”multipart/form-data”, does the HTML upload that to some previously specified directory on the web server? If so, where is that specified? How can one know where that is so that one can make sure the directory exists? Do I need to be concerned about this?

Clarification would be appreciated.

July 27, 2009 at 4:15 pm
(23) Jake says:

Hi,

Here’s the script I am using for my file upload:

I want to send the user who has uploaded their file to a different page once completed. What codes can I use as I haven’t a clue on php.

Any help would be hugely appreciated!

Thanks

December 23, 2009 at 11:06 am
(24) JackW says:

The code supplied in the tutorial works like a charm on a Linux server. CHMOD setting is not relevant. I am using similar code with safeguards built in on several sites. I have been trying to write code to do the same thing on a windows server and get this error message.

Warning: move_uploaded_file(upload/100_0095.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\Hosting\5310323\html\test\upload.php on line 19

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move ‘D:\Temp\php\php4DAB.tmp’ to ‘upload/100_0095.jpg’ in D:\Hosting\5310323\html\test\upload.php on line 19
Sorry, there was a problem uploading your file.

Any help will be appreciated.

February 11, 2010 at 3:32 pm
(25) Jon says:

This is a great script. However, I want to allow several image file types, i.e. gif, jpg, tif

How do I modify the code to do so?

if (!($uploaded_type==”image/gif”)) {
echo “You may only upload GIF files.”;
$ok=0;
}

February 16, 2010 at 2:31 am
(26) Jim says:

I have the script working great….but I need to be able to have the option to upload more than one file at a time. When I try to copy the code in the html file, only 1 file is uploaded.

help please.

February 16, 2010 at 2:36 am
(27) Jim says:

For those who would like to redirect after an upload, add this code after the “?>” tag

[code]

Back to Uploader
[/code]

February 16, 2010 at 2:38 am
(28) Jim says:

[a href="http://yoursite.com/">Back to Uploader

If the forum admin could delete the post previous to this, I would appreciate it.

May 22, 2010 at 4:13 am
(29) Richard says:

To redirect back to your index page, just add this script before the initial php code:

NOTE: Change the “index.html” to your homepage or whatever page you like and there you have it :P

December 10, 2012 at 6:45 am
(30) Austin says:

Am using a local server xampp, the come since to be uploading but am getting An error message once I prieview the page, Notice: undefined index: uploaded in c:\xampp\htdoCs\folder\upload on line 9 and another error. Notice: undefined Variable: uploaded_size in c:\xampp\htdoCs\folder\upload on line 13. And also. Notice: undefined Variable: uploaded_type in c:\xampp\htdoCs\folder\upload on line 21 and Notice: undefined index: uploaded in c:\xampp\htdoCs\folder\upload on line 36. Sorry, theRe was a problem uploading. Your file. But when I upload , the code work and still display alone two error line 13 And. 21. Please help

February 1, 2013 at 8:07 pm
(31) Zoila says:

Excellent post. I am experiencing many of these issues as well.
.

December 1, 2013 at 9:10 pm
(32) Lizz Tayler says:

I always spent my half an hour to read this weblog’s content
everyday along with a mug of coffee.

Have a look at my web-site <a href=”https://vimeo.com/80194669″>Lizz Tayler</a>

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>
  1. About.com
  2. Computing
  3. PHP / MySQL

©2014 About.com. All rights reserved.