Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become an Expert!

Join 244,173 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,340 people online right now. Registration is fast and FREE... Join Now!




Upload A File From A Form

 
Reply to this topicStart new topic

> Upload A File From A Form

Rating  5
skyhawk133
Group Icon



post 9 Dec, 2004 - 11:42 AM
Post #1


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
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

AYYASH
*



post 29 Mar, 2006 - 10:00 AM
Post #2
Question about $_SERVER['PHP_SELF'].

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

Thanks
Go to the top of the page
+Quote Post

snoj
Group Icon



post 29 Mar, 2006 - 11:18 AM
Post #3
It just means that you don't have to edit the "action" attribute to reflect the name you give the file.
Go to the top of the page
+Quote Post

AYYASH
*



post 30 Mar, 2006 - 08:22 PM
Post #4
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

This post has been edited by AYYASH: 30 Mar, 2006 - 08:23 PM
Go to the top of the page
+Quote Post

snoj
Group Icon



post 10 Jul, 2006 - 10:20 AM
Post #5
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.
Go to the top of the page
+Quote Post

Devenio
*



post 16 Feb, 2007 - 06:03 AM
Post #6
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
Go to the top of the page
+Quote Post

AYYASH
*



post 16 Feb, 2007 - 11:24 PM
Post #7
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.
Go to the top of the page
+Quote Post

Katrina102nazo
*



post 2 Nov, 2007 - 08:53 PM
Post #8
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

This post has been edited by Katrina102nazo: 2 Nov, 2007 - 08:58 PM
Go to the top of the page
+Quote Post

frafri
*



post 22 Jan, 2008 - 07:34 PM
Post #9
i look in the folder images but the picture that i tried to upload is not there =/

thanks
Go to the top of the page
+Quote Post

no2pencil
Group Icon



post 25 Jan, 2008 - 05:40 AM
Post #10
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?
Go to the top of the page
+Quote Post

imaro
*



post 17 Feb, 2008 - 12:09 AM
Post #11


thank you very much...
it really help me

This post has been edited by imaro: 17 Feb, 2008 - 12:10 AM
Go to the top of the page
+Quote Post

we_undertaker
*



post 6 May, 2008 - 03:28 AM
Post #12
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
Go to the top of the page
+Quote Post

krain
*



post 28 Sep, 2008 - 04:45 PM
Post #13
where the picture i upload go....the folder images the picture i upload is not there

.
any one can tell me what is the problem.?
Go to the top of the page
+Quote Post

gothik12
Group Icon



post 14 Nov, 2008 - 06:48 AM
Post #14
Thanks a lot. I have finally found a code for uploading a file that works!
Go to the top of the page
+Quote Post

Kirth
**



post 4 Jan, 2009 - 11:12 AM
Post #15
Someone me wondering. Is there an actual filesize limit? I've been told this is 8mb.
Go to the top of the page
+Quote Post

dargo
*



post 4 Mar, 2009 - 03:03 AM
Post #16
Thanks for a great bit of code that works just fine.

Is there a way to store the filename in a variable for later use?

CODE
$filename = mysql_real_escape_string($_POST['userfile']);


And that is empty.

I am a noob at PHP so please forgive me if this is a dumb question but I just can't work it out.

TIA
Go to the top of the page
+Quote Post

dargo
*



post 5 Mar, 2009 - 12:40 AM
Post #17
I managed to find the answer myself. If anyone else is interested the code you need is

CODE
$filename = mysql_real_escape_string($_FILES["userfile"])["name"]);
Go to the top of the page
+Quote Post

kcvontop
*



post 29 Apr, 2009 - 03:05 PM
Post #18
Thanks Skyhawk. The upload scipt worked fine. icon_up.gif

Thanks Skyhawk. The upload script worked fine. icon_up.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 7/4/09 05:42AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month