Improved PHP Script to Upload Images

We have already posted PHP script to upload images. This is an improved script with little changes, this script will change the name of file and by appending some additional random numbers to the original name.  By doing this we are getting an advantage that we’ll never face a problem of “file already exists on server/folder”. Another change that I made in this script is that it will show thumbnail of the uploaded picture, user would be able to see which picture he has uploaded. You can use this script in your web projects especially when you want user to upload only images like profile pictures etc.

file uploader

                            DOWNLOAD

I have created two files “file.html” which will be used to accept file from user and “upload.php” file contains the PHP script which will rename the file (if exists) and upload it to server.

Code for File.html:

<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

Code for Upload.php:

<?php
$info_file_exts = array("jpg", "jpeg", "gif", "png");
$info_upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($info_upload_exts, $info_file_exts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$info_file_exts = array("jpg", "jpeg", "gif", "png");
if($info_file_exts[0]=='jpg'){
$ink=explode('.'.$info_file_exts[0],$_FILES["file"]["name"]);
$time=time();
$info_new_file_name =$ink[0].'_'.$time.'.'.$info_file_exts[0];
}
else if($info_file_exts[1]=='jpeg'){
$ink=explode('.'.$info_file_exts[1],$_FILES["file"]["name"]);
$time=time();
$info_new_file_name =$ink[0].'_'.$time.'.'.$info_file_exts[0];
}
else if($info_file_exts[2]=='gif'){
$ink=explode('.'.$info_file_exts[2],$_FILES["file"]["name"]);
$time=time();
$info_new_file_name =$ink[0].'_'.$time.'.'.$info_file_exts[0];
}
else if($info_file_exts[3]=='png'){
$ink=explode('.'.$info_file_exts[3],$_FILES["file"]["name"]);
$time=time();
$info_new_file_name =$ink[0].'_'.$time.'.'.$info_file_exts[0];
}

$info_old_file_path="c:\wamp\www\upload/newupload/" . $_FILES["file"]["name"];
$info_new_file_path="c:\wamp\www\upload/newupload/" . $info_new_file_name;

move_uploaded_file($_FILES["file"]["tmp_name"],
"c:\wamp\www\upload/newupload/" . $_FILES["file"]["name"]);
// echo "<div class='sucess'>"."Stored in: " . "c:\wamp\www\upload/newupload/" . $_FILES["file"]["name"]."</div>";
$name=rename($info_old_file_path,$info_new_file_path);
echo "<img src='http://127.0.0.1/upload/newupload/" . $info_new_file_name."' style='width:300; height:250px;' />";
echo "</br>Your file is ".$_FILES['file']['name']."  uploaded.";
}
}

else
{
echo "<div class='error'>Invalid file</div>";
}
?>

Change the path of the upload folder as per your requirement. If you need to add more file types to be supported in your project then simply you can add them in the script.

About satyendra

satyendra
I am a PHP and WordPress developer. I love to code and build something useful.

Related posts:

5 Responses so far.

  1. Good script, but there’s a mistake in it at this part :

    ######
    if($info_file_exts[0]==’jpg’){
    $ink=explode(‘.’.$info_file_exts[0],$_FILES[“file”][“name”]);
    $time=time();
    $info_new_file_name =$ink[0].’_’.$time.’.’.$info_file_exts[0];
    }
    else if($info_file_exts[1]==’jpeg’){
    $ink=explode(‘.’.$info_file_exts[1],$_FILES[“file”][“name”]);
    $time=time();
    $info_new_file_name =$ink[0].’_’.$time.’.’.$info_file_exts[0];
    }
    else if($info_file_exts[2]==’gif’){
    $ink=explode(‘.’.$info_file_exts[2],$_FILES[“file”][“name”]);
    $time=time();
    $info_new_file_name =$ink[0].’_’.$time.’.’.$info_file_exts[0];
    }
    else if($info_file_exts[3]==’png’){
    $ink=explode(‘.’.$info_file_exts[3],$_FILES[“file”][“name”]);
    $time=time();
    $info_new_file_name =$ink[0].’_’.$time.’.’.$info_file_exts[0];
    }

    #####

    In order to make it work properly, in ALL the if or else if conditions, just change the variable for $info_upload_exts (no array), else, it’ll always fall in the first IF statement because you’re asking PHP if the first position of your allowed extensions array is a JPG, which is always the case… So your PNG will be renamed : blabla.png_DATE.jpg !

    1. Oh and also, in if actions, you should replace the : $info_file_exts[0] (which is always jpeg in every case, that doesnt makes sense since it’ll corrupt your PNG or GIF and put them in jpg), you should just use the $info_upload_exts since you just confirmed it was the one you need, in addition, you don’t have to redefine the array on line 17 since you already created the same variable before on line 2, you still can use it !

Comments are closed.