PHP Script to Upload Images to Server


<html>
<head>
<title>PHP Script to Upload png, jpg, jpeg, bmp, gif Images to the
server</title>
</head>
<body>
<div><?php
if ( isset( $_FILES['imgUpload'] ) ) {

	if ( ($_FILES['imgUpload']['type'] == "image/png") ||
	($_FILES['imgUpload']['type'] == "image/PNG") ||
	($_FILES['imgUpload']['type'] == "image/jpg") ||
	($_FILES['imgUpload']['type'] == "image/JPG") ||
	($_FILES['imgUpload']['type'] == "image/JPEG") ||
	($_FILES['imgUpload']['type'] == "image/jpeg") ||
	($_FILES['imgUpload']['type'] == "image/gif") ||
	($_FILES['imgUpload']['type'] == "image/GIF")) {

		$source_file = $_FILES['imgUpload']['tmp_name'];
		$dest_file = "upload/".$_FILES['imgUpload']['name'];
			

		if (file_exists($dest_file)) {

			echo "The file name already exists!!";
		}
		else {

			move_uploaded_file( $source_file, $dest_file )

			or die ("Error occured !! Couldn't copy");


			if($_FILES['imgUpload']['error'] == 0) {

				echo "File was successfully uploaded!! <br/><br/>";
				echo "<b><u>Details : </u></b><br/>";
				echo "File Name : ".$_FILES['imgUpload']['name']."<br.>"."<br/>";
				echo "File Size : ".$_FILES['imgUpload']['size']." bytes"."<br/>";
				echo "File Extension : ".$_FILES['imgUpload']['type']."<br/>";
				echo "File Temp Location and Temp name : ".$_FILES['imgUpload']['tmp_name']."<br/>";
				echo "Final location : upload/".$_FILES['imgUpload']['name']."<br/>";

			}
		}

	}
	else {

		if ( ($_FILES['imgUpload']['type'] != "image/png") ||
		($_FILES['imgUpload']['type'] != "image/PNG") ||
		($_FILES['imgUpload']['type'] != "image/jpg") ||
		($_FILES['imgUpload']['type'] != "image/JPG") ||
		($_FILES['imgUpload']['type'] != "image/JPEG") ||
		($_FILES['imgUpload']['type'] != "image/jpeg") ||
		($_FILES['imgUpload']['type'] != "image/gif") ||
		($_FILES['imgUpload']['type'] != "image/GIF")) {

			echo "Error occured while uploading file : ".$_FILES['imgUpload']['name']."<br/>";
			echo "Invalid Image file extension!!"."<br/>";
			echo "Error Code : ".$_FILES['imgUpload']['error']."<br/>";


		}


	}

}
?></div>

<form enctype="multipart/form-data"
	action="<?php print $_SERVER['PHP_SELF']?>" method="post">
<p><input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> <input
	type="file" name="imgUpload" /><br />
<input type="submit" value="upload!" /></p>
</form>
</body>
</html>

HTML :

Create a element in HTML section with attribute enctype="multipart/form-data"

We need to add the encoding type to encode the POST request file when the upload button is being clicked.

As we are trying to POST a form with a file it is important to tell the HTTP protocol that we are sending a file having certain characteristics. Note if you do not provide enctype="multipart/form-data" HTML request will consider it as application/x-www-form-urlencoded which is default.

When we use multipart/form-data, the file is first stored in a temporary location on the server until the script is completely executed. The form is submitted using $PHP_SELF, you may get HTTP errors like 403: Access violation if PHP server is not configured properly.

To resolve this issue check php.ini file and look for file_uploads variable, its value should be ON and not OFF.

Next we have a hidden input form element with type="file", here we can set the max file size in bytes. Note this size should be equal to or less than the upload_max_filesize defined in PHP servers php.ini file.

Next we have a submit button.

PHP Script

As we are uploading only images file we need to check for the file extensions before uploading the file to a permanent location if the file contains extensions other than jpg, png, gif, jpeg we would throw an error message. we can get the file type using $_FILES['imgUpload']['type']

Note that if you do not have write access to the destination directory you may get an error for move_uploaded_file()

Warning: move_uploaded_file(upload/screen dimentions output.png): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/android/php_examples/1_upload/file_upload.php on line 33

Warning: move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/phpQlVGeb' to 'upload/screen dimentions output.png' in /Applications/XAMPP/xamppfiles/htdocs/android/php_examples/1_upload/file_upload.php on line 33

Warning: getimagesize(upload/screen dimentions output.png): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/android/php_examples/1_upload/file_upload.php on line 34 uploaded image

We check if the file already exists at the server by using if (file_exists($dest_file)) conditional check.

We can show details regarding the uploaded file :

File name : $_FILES['imgUpload']['name']
File size : $_FILES['imgUpload']['size']
File extension : $_FILES['imgUpload']['type']
File Temp location : $_FILES['imgUpload']['tmp_name']
File Upload location :$_FILES['imgUpload']['name']
Output :
php-file-upload-script.png
php-file-upload-script.png
Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency