• (2 Pages)
  • +
  • 1
  • 2

Upload A File From A Form Rate Topic: ****- 3 Votes

#1 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Posted 09 December 2004 - 12:42 PM

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:

<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

Is This A Good Question/Topic? 0
  • +

Replies To: Upload A File From A Form

#2 AYYASH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-March 06

Posted 29 March 2006 - 11:00 AM

Question about $_SERVER['PHP_SELF'].

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

Thanks
Was This Post Helpful? 0
  • +
  • -

#3 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Posted 29 March 2006 - 12:18 PM

It just means that you don't have to edit the "action" attribute to reflect the name you give the file.
Was This Post Helpful? 0
  • +
  • -

#4 AYYASH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-March 06

Posted 30 March 2006 - 09:22 PM

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 March 2006 - 09:23 PM

Was This Post Helpful? 0
  • +
  • -

#5 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Posted 10 July 2006 - 11:20 AM

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.
Was This Post Helpful? 0
  • +
  • -

#6 Devenio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 12-February 07

Posted 16 February 2007 - 07:03 AM

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

but i get an error as
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

Was This Post Helpful? 0
  • +
  • -

#7 AYYASH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-March 06

Posted 17 February 2007 - 12:24 AM

View PostDevenio, on 16 Feb, 2007 - 07:03 AM, said:

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

but i get an error as
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.
Was This Post Helpful? 0
  • +
  • -

#8 Katrina102nazo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-October 07

Posted 02 November 2007 - 09:53 PM

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: 02 November 2007 - 09:58 PM

Was This Post Helpful? 0
  • +
  • -

#9 frafri  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 12-October 07

Posted 22 January 2008 - 08:34 PM

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

thanks
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5392
  • View blog
  • Posts: 27,387
  • Joined: 10-May 07

Posted 25 January 2008 - 06:40 AM

View Postfrafri, on 22 Jan, 2008 - 08:34 PM, said:

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?
Was This Post Helpful? 0
  • +
  • -

#11 imaro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-February 08

Posted 17 February 2008 - 01:09 AM

thank you very much...
it really help me

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

Was This Post Helpful? 0
  • +
  • -

#12 we_undertaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 06-May 08

Posted 06 May 2008 - 04:28 AM

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
Was This Post Helpful? 0
  • +
  • -

#13 krain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 19-July 08

Posted 28 September 2008 - 05:45 PM

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.?
Was This Post Helpful? 0
  • +
  • -

#14 gothik12  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 207
  • Joined: 10-November 07

Posted 14 November 2008 - 07:48 AM

Thanks a lot. I have finally found a code for uploading a file that works!
Was This Post Helpful? 0
  • +
  • -

#15 Kirth  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 157
  • Joined: 09-July 08

Posted 04 January 2009 - 12:12 PM

Someone me wondering. Is there an actual filesize limit? I've been told this is 8mb.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2