<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 ScriptAs 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 :

- How to destroy PHP session()
- PHP Code for sending Emails
- 403 forbidden error for Image
- macOS - cannot calculate MAC address: Using fd 9 hv_vm_create HV_ERROR [PHP XAMPP]
- Upload Pdf file using PHP Script
- PHP header location function not called
- Copy file from one directory to other in Php
- PHP.ini: How to Remove URL Forward Slash Before Single or Double Quotes
- PHP Base64 encoding decoding a variable
- PHP Fatal error : Call to a member function bind_param() on a non-object
- PHP Script to Upload Images to Server
- Upload docx file using PHP script
- How to Pretty Print JSON in PHP
- PHP Warning: Cannot modify header information - headers already sent
- PHP drag and drop file upload tutorial using dropzone.js
- Failed to load resource: net::ERR_CACHE_MISS PHP
- PHP 301 Redirect Permanently
- Call PHP function on Button click using jquery ajax
- Installing vue.js in Laravel 8
- Save current timestamp in MySQL using PHP mysqi binding
- Delete file using PHP code : unlink()
- Jupyter Notebook: 500 Internal Server Error - nbconvert failed: xelatex not found on PATH - Python
- Convert String Date to Date Object in Java - Java
- How to turn off Stage Manager - macOS Ventura - MacOS
- Read a file line by line in Python Program - Python
- JDBCTemplate Querying Examples with Spring Boot 3 - Java
- Display ls command file sizes in KB (kilobytes) MB (megabytes) or GB (gigabytes) [Linux/macOS] - MacOS
- Change Title text for Android Activity using java code - Android
- Java JDBC Connection with Database using SSL (https) URL - Java