Image File Upload Script In PHP

This tutorial will tell you how to upload images using PHP script. Developers need this because they may need to upload files in their projects. For example in registration page you want users to upload their picture for profile.

Check Here to see Live Demo

To implement image upload code in your web projects you need to create two file one for taking image as input and another for php script to upload image in server.

  •  file.html     : This file will ask user to browse and select image from his system.
  •  upload.php  : This file will check the file format and size and then it will 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:

<style>
.sucess{
color:#088A08;
}
.error{
color:red;
}
</style>

<?php
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$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($upload_exts, $file_exts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
// Enter your path to upload file here
if (file_exists("c:\wamp\www\upload/newupload/" .
$_FILES["file"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
" already exists. "."</div>";
}
else
{
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>";
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
?>

Note: I tested thsi script on my local WAMP server so the path given is “c:\wamp\www\upload/newupload/”. You will have to change this path with your uploading directory path on your server.

This is a simple tutorial to upload file. I will post next tutorial to upload file using jQuery and ajax for better performance.

About sanjeev

sanjeev
Dreamer, Blogger and Thinker. I love to help people in solving their problems. You can also join me HERE

Related posts:

19 Responses so far.

  1. How can i upload more then one file.

    And during registration how can i see profile pictures?

    for example i have a page and in the page to see users that registered.
    and when i press on his picture to see profile pictures.

    1. Hi Kumar,
      What editing you want to do in images? Do you mean cropping of images with php and then saving them to database? We will post a tutorial soon for this.

  2. How can i upload more then one file.

    And during registration how can i see profile pictures?

    for example i have a page and in the page to see users that registered.
    and when i press on his picture to see profile pictures.

  3. all code is successfully run. file is upload in given path but i got an error which is show in the below.please give me solution with explanation thanks in advance..
    error is-
    Strict standards: Only variables should be passed by reference in C:\wamp\www\brijeshrana.in\admin\front_image_upload.php on line 15

    line no. 15 is- $upload_exst = end(explode(“.”,$_FILES[“image”][“name”]));

  4. Hi Sanjeev

    Nice way to write the codes, simple and easy to implement.

    However I need one help .. how can i upload the image with unique id using your codes.

    unique id will be something like this : date(“Ymdhis”);
    $ip = $_SERVER[‘REMOTE_ADDR’];
    $orderid = “$stamp-$ip”;
    $orderid = str_replace(“.”, “”, “$orderid”);
    echo($orderid);

    it will be of great help if you can suggest something
    rgds
    UB

  5. hi frd..its very nice..thanks…pls send image resize while uploading in php code in that above program connection

Comments are closed.