File Upload Facility Using PHP

 

The following script is useful if you would like a user to upload any type of file to your site. To add file restrictions is simple. If you would like help please contact me.

The PHP script itself, contains its own error catching code, which will activate if a user makes a mistake e.g. if a filename is not entered in the filename text area. The script also contains the if(!isset($_POST["submit"])) which checks if the submit button has been clicked.

If you would like the image within the form, simply right click (CTRL + click for MAC), select "Save As" and save the GIF image to the appropriate folder on your website.

Please note: The form itself has been disabled. I would also like to remind everyone, that a file upload facility such as this should not be placed on a web page viewable to the public.

 

 

Example

File Upload
 

 

When the user submits a file, the PHP script will display the following:

File to upload: yourFile.txt
Upload in progress...................
Server location: location/yourFile.txt
Type: text/plain
Size: 0.146484375 Kb
File upload complete!... Date: 2007-05-05

 

Script Source

 

<The Form & PHP Script >

<fieldset>
<legend>File Upload</legend>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />&nbsp;<img src="disk.gif" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<br />
<?php
// +++++++++++++++++++++++++++++++++++
// + Script by www.wizzardweb.co.uk
// + Copyright 2007
// + Please leave in place
// +++++++++++++++++++++++++++++++++++

if(!isset($_POST["submit"])) { // check if the submit button has been clicked

} elseif ($_FILES["file"]["error"] > 0) { // if true (error) print the code below

echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else // if the above code returns false print the following
{
echo "File to upload: " . $_FILES["file"]["name"] . "<br />";
echo "Upload in progress...................";
sleep(3);
echo "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo "*** WARNING! ***<br>";
echo "The file " . $_FILES["file"]["name"] . " already exists. ";
}
else
{
echo "Server location: " . "upload/" . $_FILES["file"]["name"] ."<br/>";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "File upload complete!"."... Date: " .date("Y-m-d");

}
}

?>
</fieldset>

Please let me know if this script was helpful to you.

[rating]