Basically you have a HTML form with an input field type of 'FILE' (ie. <input type="file" name="file_to_upload" />) and want a PHP file to process this. This example applies to a Linux Apache MySQL PHP (LAMP) environment.
The Solution
1. The first thing to do is check that your HTML form is setup to do this:
The form above will post to itself but more importantly is the enctype="multipart/form-data" without which your file upload processing won't work. The name of the file is important but you can call it what you want, in my case I've called it 'uploaded_file'.
2. Check that the destination directory is writable by Apache.
This is the full path to the folder that you will be uploading the files to and should be 755 (ie. rwxr-xr-x). The change ownership command in shell is 'chown -R apache:apache
3. Check that your PHP ini specifies a reasonable maximum file size limit.
By default this is 2Mb. In my example, I added the hidden input type MAX_FILE_SIZE which is ignored by more recent browsers. If you need more, you need to edit your PHP.ini file.
4. Test your PHP file can process the data
Add the following code to the receiving PHP file:
echo "Upload: " . $_FILES["uploaded_file"]["name"] . ""; echo "Type: " . $_FILES["uploaded_file"]["type"] . ""; echo "Size: " . ($_FILES["uploaded_file"]["size"] / 1024) . " Kb"; echo "Temp file: " . $_FILES["uploaded_file"]["tmp_name"] . "";
- echo "Upload: " . $_FILES["uploaded_file"]["name"] . "";
- echo "Type: " . $_FILES["uploaded_file"]["type"] . "";
- echo "Size: " . ($_FILES["uploaded_file"]["size"] / 1024) . " Kb";
- echo "Temp file: " . $_FILES["uploaded_file"]["tmp_name"] . "";
5. Form processing works so now test the actual file upload
if (file_exists("" . $_FILES["uploaded_file"]["name"])) { echo "".$_FILES["uploaded_file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "" .$_FILES["uploaded_file"]["name"]); echo "Stored in: " . "" . $_FILES["uploaded_file"]["name"]; chmod("" . $_FILES["uploaded_file"]["name"], 0755); }
- if (file_exists("" . $_FILES["uploaded_file"]["name"])) { echo "".$_FILES["uploaded_file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "" .$_FILES["uploaded_file"]["name"]); echo "Stored in: " . "" . $_FILES["uploaded_file"]["name"]; chmod("" . $_FILES["uploaded_file"]["name"], 0755); }
My Full Snippet
This is the code on the receiving PHP file:
/** STUFF FOR FILE UPLOAD ********************************************************/ if ( ( ($_FILES["uploaded_file"]["type"] == "image/gif") || ($_FILES["uploaded_file"]["type"] == "image/jpg") || ($_FILES["uploaded_file"]["type"] == "image/pjpeg") || ($_FILES["uploaded_file"]["type"] == "image/jpeg") || ($_FILES["uploaded_file"]["type"] == "image/png") || ($_FILES["uploaded_file"]["type"] == "image/bmp") ) && ($_FILES["uploaded_file"]["size"] < 20000) ) { if ($_FILES["uploaded_file"]["error"] > 0) { echo "Return Code: " . $_FILES["uploaded_file"]["error"] . ""; } else { echo "Upload: " . $_FILES["uploaded_file"]["name"] . ""; echo "Type: " . $_FILES["uploaded_file"]["type"] . ""; echo "Size: " . ($_FILES["uploaded_file"]["size"] / 1024) . " Kb"; echo "Temp file: " . $_FILES["uploaded_file"]["tmp_name"] . ""; if (file_exists("/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"])) { echo "File /var/www/html/images/bu_roomassets/".$_FILES["uploaded_file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"]); echo "Stored in: " . "/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"]; chmod("/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"], 0755); } } } else { if (trim($_FILES["uploaded_file"]["type"])=="") { echo "No file submitted for upload."; } else { echo "Invalid file type!!! You tried to upload a file type of ".$_FILES["uploaded_file"]["type"]; } }
- /** STUFF for FILE UPLOAD ********************************************************/
- if (
- (
- ($_FILES["uploaded_file"]["type"] == "image/gif") ||
- ($_FILES["uploaded_file"]["type"] == "image/jpg") ||
- ($_FILES["uploaded_file"]["type"] == "image/pjpeg") ||
- ($_FILES["uploaded_file"]["type"] == "image/jpeg") ||
- ($_FILES["uploaded_file"]["type"] == "image/png") ||
- ($_FILES["uploaded_file"]["type"] == "image/bmp")
- ) &&
- ($_FILES["uploaded_file"]["size"] < 20000)
- ) {
- if ($_FILES["uploaded_file"]["error"] > 0) {
- echo "Return Code: " . $_FILES["uploaded_file"]["error"] . "";
- } else {
- echo "Upload: " . $_FILES["uploaded_file"]["name"] . "";
- echo "Type: " . $_FILES["uploaded_file"]["type"] . "";
- echo "Size: " . ($_FILES["uploaded_file"]["size"] / 1024) . " Kb";
- echo "Temp file: " . $_FILES["uploaded_file"]["tmp_name"] . "";
- if (file_exists("/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"])) {
- echo "File /var/www/html/images/bu_roomassets/".$_FILES["uploaded_file"]["name"] . " already exists. ";
- } else {
- move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"]);
- echo "Stored in: " . "/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"];
- chmod("/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"], 0755);
- }
- }
- } else {
- if (trim($_FILES["uploaded_file"]["type"])=="") {
- echo "No file submitted for upload.";
- } else {
- echo "Invalid file type!!! You tried to upload a file type of ".$_FILES["uploaded_file"]["type"];
- }
- }