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.




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 >

<legend>File Upload</legend>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
<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" />
<br />
// +++++++++++++++++++++++++++++++++++
// + Script by
// + 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...................";
echo "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
echo "*** WARNING! ***<br>";
echo "The file " . $_FILES["file"]["name"] . " already exists. ";
echo "Server location: " . "upload/" . $_FILES["file"]["name"] ."<br/>";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
"upload/" . $_FILES["file"]["name"]);
echo "File upload complete!"."... Date: " .date("Y-m-d");



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