Script for PHP upload image file to Server using move_uploaded_file()

Script for PHP upload image file to Server using move_uploaded_file()

PHP is a high level server side programming language. To upload an image file first you need to update the php.ini file. Open the php.ini file. Search “file_uploads”. Update the line “file_uploads = On”.

Then to upload the file you need to Create HTML. Look at the example below. Here I have a input type=”file” control & a submit button. Both these controls are inside the form element. With form I am using post method to send my image files to the server. Finally after successful uploading showing File Name, Size & Type to the user using a UL li element.

To restrict user on file type to upload I created an array “expensions”. Here you can declare any specific file type you want to allow the user. In this example I allowed “jpeg”,”jpg”,”png”.

During file upload to track the errors I am with one more array “errorlog”. In case of a failure I am displaying the error details using echo function.

Script for PHP upload image


$errorlog = array();

$fileSize = $_FILES['uploader']['size'];
$fileType = $_FILES['uploader']['type'];
$fileTemp = $_FILES['uploader']['tmp_name'];
$fileName = $_FILES['uploader']['name'];

$fileExtension = strtolower(end(explode('.',$_FILES['uploader']['name'])));

$expensions= array("jpeg","jpg","png");

if(in_array($fileExtension,$expensions)=== false){
$errorlog[] = "This file type is not allowed, Select a JPEG or PNG file.";

if($fileSize > 2097152){
$errorlog[] = 'Your file size is more than 2 MB. Upload a file less then 2 MB.';

echo "Successfully Uploaded.";
else {
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="uploader" />
<input type="submit"/>
<li>Sent File - <?php echo $_FILES['uploader']['name']; ?></li>
<li>File Size - <?php echo $_FILES['uploader']['size']; ?></li>
<li>File Type - <?php echo $_FILES['uploader']['type']; ?></li>