Using a HTML form and PHP to upload a file

The Issue

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:
copyraw
...   ...
  1.  ...   ... 

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: 

copyraw
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"] . "";
  1.  echo "Upload: " . $_FILES["uploaded_file"]["name"] . ""; 
  2.  echo "Type: " . $_FILES["uploaded_file"]["type"] . ""; 
  3.  echo "Size: " . ($_FILES["uploaded_file"]["size"] / 1024) . " Kb"; 
  4.  echo "Temp file: " . $_FILES["uploaded_file"]["tmp_name"] . ""; 
The key of the above array is the name of the variable you passed in your HTML form.  If the above yields NO empty data (and size is not 0) then it was successful.  If not, then either your system isn't accepting the file type you are trying to upload, or the file is too big, or this feature just isn't available to you.
 
Note: The temporary file gets deleted when the script finishes.  So the file shouldn't exist if you check after the script has completed.  Do the following step to keep the file in a specified folder.
 
 

5. Form processing works so now test the actual file upload

Add the following code after the above code sample in step #4:
copyraw
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); }
  1.  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)} 
Obviously replace with your full path (eg. /var/www/html/images).

 

My Full Snippet

This is the code on the receiving PHP file:

copyraw
/** 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"];
	}
   }
  1.  /** STUFF for FILE UPLOAD ********************************************************/ 
  2.  if ( 
  3.      ( 
  4.          ($_FILES["uploaded_file"]["type"] == "image/gif") || 
  5.          ($_FILES["uploaded_file"]["type"] == "image/jpg") || 
  6.          ($_FILES["uploaded_file"]["type"] == "image/pjpeg") || 
  7.          ($_FILES["uploaded_file"]["type"] == "image/jpeg") || 
  8.          ($_FILES["uploaded_file"]["type"] == "image/png") || 
  9.          ($_FILES["uploaded_file"]["type"] == "image/bmp") 
  10.      ) && 
  11.      ($_FILES["uploaded_file"]["size"] < 20000) 
  12.     ) { 
  13.      if ($_FILES["uploaded_file"]["error"] > 0) { 
  14.          echo "Return Code: " . $_FILES["uploaded_file"]["error"] . ""
  15.      } else { 
  16.          echo "Upload: " . $_FILES["uploaded_file"]["name"] . ""
  17.          echo "Type: " . $_FILES["uploaded_file"]["type"] . ""
  18.          echo "Size: " . ($_FILES["uploaded_file"]["size"] / 1024) . " Kb"
  19.          echo "Temp file: " . $_FILES["uploaded_file"]["tmp_name"] . ""
  20.          if (file_exists("/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"])) { 
  21.              echo "File /var/www/html/images/bu_roomassets/".$_FILES["uploaded_file"]["name"] . " already exists. "
  22.          } else { 
  23.              move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"])
  24.              echo "Stored in: " . "/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"]
  25.              chmod("/var/www/html/images/bu_roomassets/" . $_FILES["uploaded_file"]["name"], 0755)
  26.          } 
  27.      } 
  28.     } else { 
  29.      if (trim($_FILES["uploaded_file"]["type"])=="") { 
  30.          echo "No file submitted for upload."
  31.      } else { 
  32.          echo "Invalid file type!!! You tried to upload a file type of ".$_FILES["uploaded_file"]["type"]
  33.      } 
  34.     } 
Category: Personal Home Page :: Article: 177

Add comment

Your rating:

Submit

Accreditation

Badge - Zoho Creator Certified Developer Associate
Badge - Zoho Deluge Certified Developer
Badge - Certified Zoho CRM Developer

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF

Please publish modules in offcanvas position.