Uploading images to web server is a common function in web development, in this tutorial, we will introduce how to upload images using php.
You can learn how to use php to upload images step by step.
Create a form to upload images
We will use html form to upload images. In order to send image data, we should set method=”post” and enctype=”multipart/form-data”. Here is an example code:
<form action="upload.php" method="post" enctype="multipart/form-data"> <h2>Upload File</h2> <label for="fileSelect">Filename:</label> <input type="file" name="photo" id="fileSelect"> <input type="submit" name="submit" value="Upload"> <p><strong>Note:</strong> Only .jpg, .jpeg, .gif, .png formats allowed to a max size of 5 MB.</p> </form>
You can not use get method to upload images, because http get method only can send a little amount of data.
This code will send image data to upload.php, upload.php will receive image data and save it to web server.
How to receive image data and save image in upload.php?
upload.php will use $_FILES to receive image data and other image information, such as image size, image type and image name. More details on $_FILES in:
PHP $_FILES Variable – A Beginner Guide
upload.php has received image data, it will save this uploaded image in a temporary path, we will use move_uploaded_file() to move this temporary file to our destination folder.
Make sure the uploaded file is an image
In order to upload images, we should make sure the uploaded file is an image, we should check file name or file mime type.
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"); $filename = $_FILES["photo"]["name"]; $filetype = $_FILES["photo"]["type"]; $filesize = $_FILES["photo"]["size"]; // Verify file extension $ext = pathinfo($filename, PATHINFO_EXTENSION); if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
Make sure the file size is valid
We should limit the max size of an image, which is helpful to your web server.
$maxsize = 5 * 1024 * 1024; if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
Save uploaded image
In this example, we will upload images to upload folder, we can move_uploaded_file() to save uploaded files.
if(in_array($filetype, $allowed)){ // Check whether file exists before uploading it if(file_exists("upload/" . $filename)){ echo $filename . " is already exists."; } else{ //echo $_FILES["photo"]["tmp_name"]; if(move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $filename)){ // may error echo "Your file was uploaded successfully."; }else{ echo "Error: fail to move file to upload/"; } } } else{ echo "Error: There was a problem uploading your file. Please try again."; }
The full code of upload.php is here:
<?php // Check if the form was submitted if($_SERVER["REQUEST_METHOD"] == "POST"){ // Check if file was uploaded without errors if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){ $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"); $filename = $_FILES["photo"]["name"]; $filetype = $_FILES["photo"]["type"]; $filesize = $_FILES["photo"]["size"]; // Verify file extension $ext = pathinfo($filename, PATHINFO_EXTENSION); if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format."); // Verify file size - 5MB maximum $maxsize = 5 * 1024 * 1024; if($filesize > $maxsize) die("Error: File size is larger than the allowed limit."); // Verify MYME type of the file if(in_array($filetype, $allowed)){ // Check whether file exists before uploading it if(file_exists("upload/" . $filename)){ echo $filename . " is already exists."; } else{ //echo $_FILES["photo"]["tmp_name"]; if(move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $filename)){ // may error echo "Your file was uploaded successfully."; }else{ echo "Error: fail to move file to upload/"; } } } else{ echo "Error: There was a problem uploading your file. Please try again."; } } else{ echo "Error: " . $_FILES["photo"]["error"]; } } ?>