• (2 Pages)
  • +
  • 1
  • 2

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

#16 dargo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-March 09

Posted 04 March 2009 - 04:03 AM

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?

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

#17 dargo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-March 09

Posted 05 March 2009 - 01:40 AM

I managed to find the answer myself. If anyone else is interested the code you need is

 $filename = mysql_real_escape_string($_FILES["userfile"])["name"]);

Was This Post Helpful? 0
  • +
  • -

#18 kcvontop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-April 09

Posted 29 April 2009 - 04:05 PM

Thanks Skyhawk. The upload scipt worked fine. :^:

Thanks Skyhawk. The upload script worked fine. :^:
Was This Post Helpful? 0
  • +
  • -

#19 timlarsson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-July 09

Posted 29 July 2009 - 02:23 PM

Just a minor xhtml correction:

<input type="hidden" name="MAX_FILE_SIZE" value="2048000">


should be:

<input type="hidden" name="MAX_FILE_SIZE" value="2048000" />


Also, I think parts of the code should be explained a bit more and extended.
What is $_FILES ? Can you choose a different filename? If it has the same filename, will it overwrite without asking any questions? And what about the max file size? is the value in bits or bytes, kilobytes, etc. ? and must it be 2048000, or can it be larger or smaller? And where is the max file size called in the uploading script? is it even used?

No offence with that little rant, just constructive critique to enhance the tutorial further :) I think it's really good but could use a bit of explaining!
Was This Post Helpful? 0
  • +
  • -

#20 vshall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-August 09

Posted 31 August 2009 - 05:50 AM

View Postskyhawk133, on 9 Dec, 2004 - 11:42 AM, said:

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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2