Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,399 Programmers. There are 1,455 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

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 - 12:42 PM
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


Register to Make This Ad Go Away!

AYYASH
*



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

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

Thanks

snoj
Group Icon



post 29 Mar, 2006 - 12:18 PM
Post #3
It just means that you don't have to edit the "action" attribute to reflect the name you give the file.

AYYASH
*



post 30 Mar, 2006 - 09: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 - 09:23 PM

snoj
Group Icon



post 10 Jul, 2006 - 11: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.

Devenio
*



post 16 Feb, 2007 - 07: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

AYYASH
*



post 17 Feb, 2007 - 12:24 AM
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.

Katrina102nazo
*



post 2 Nov, 2007 - 09: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 - 09:58 PM

frafri
*



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

thanks

no2pencil
Group Icon



post 25 Jan, 2008 - 06: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?

imaro
*



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


thank you very much...
it really help me

This post has been edited by imaro: 17 Feb, 2008 - 01:10 AM

we_undertaker
*



post 6 May, 2008 - 04: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


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: 5/17/08 06:14AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month