Full Version: Upload A File From A Form
Dream.In.Code > Programming Tutorials > PHP Tutorials
skyhawk133
Level of Difficulty : Beginner
Requirements : PHP

This tutorial will cover how to make a basic PHP upload script. You can easily convert or change it.

You need a PHP enabled host, the ability to upload, and HTML or PHP knowledge to change the script around to fit your needs

This is the basic code:

HTML
<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2048000">
File: <input name="userfile" type="file" /><br />
<input type="submit" value="Upload" />
</form>

<?php
if (@is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
copy($_FILES["userfile"]["tmp_name"], "/images/" . $_FILES["userfile"]["name"]);
echo "<p>File uploaded successfully.</p>";
}
?>

You can change the "/images/" to any directory you want.

You may also want to password protect the file. This is so only webmasters or selected people can view it.

[mod edit] Made a litttle change to the code to rid it of error. --hotsnoj
AYYASH
Question about $_SERVER['PHP_SELF'].

What would be the effect with it or without it in the form?

Thanks
snoj
It just means that you don't have to edit the "action" attribute to reflect the name you give the file.
AYYASH
First,

Thank you very very much for this simple and very useful uploading code.
I've tried it and had take of the $ before the /images/ folder to make it work. It shoes an error while it's there.

I have some other questions regarding this.
What if I want to update the image that I've already uploaded?
If I want to write the inage path in the data base, is this easy to do?
What if I want to have multible image uplaods in one page?

Thanks in advance
snoj
QUOTE
What if I want to update the image that I've already uploaded?
If I want to write the inage path in the data base, is this easy to do?
What if I want to have multible image uplaods in one page?


1. Just add an if block in there to check if said file exists and if it's last modified time is different than the one being uploaded.

2. The path is defined in the second part of the copy() function. You can find it there.

3. I'm not sure of the structure of the $_FILES array when there are multiples, but it should be easy to make this code into a loop to go through them all.
Devenio
Im trying this upload and i changed the images link to, "/inc/images"

but i get an error as
CODE
Warning: copy(/inc/images/Banshee2.jpg): failed to open stream: No such file or directory
in /home/www/nexus.awardspace.us/upload.php on line 9
AYYASH
QUOTE(Devenio @ 16 Feb, 2007 - 07:03 AM) *

Im trying this upload and i changed the images link to, "/inc/images"

but i get an error as
CODE
Warning: copy(/inc/images/Banshee2.jpg): failed to open stream: No such file or directory
in /home/www/nexus.awardspace.us/upload.php on line 9



Did you create the directory? If yes, then you need to CHMOD the directory to 777.
Katrina102nazo
Hi Devenio,
I tried this code, and successfully done. I dont think so there is any problem. Code is perfect. If you have still problem, i can help you just PM me
frafri
i look in the folder images but the picture that i tried to upload is not there =/

thanks
no2pencil
QUOTE(frafri @ 22 Jan, 2008 - 08:34 PM) *

i look in the folder images but the picture that i tried to upload is not there =/

thanks

Does the upload directory have write permissions for the group running your web server?
imaro


thank you very much...
it really help me
we_undertaker
can we upload video and can playback

if u know , plzz help me by provide some sample code
i am new in php , plz help me
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.