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.
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.
GOOD WORK THANKS
nice why of writing code..
really thanks
yeah it IS a really nice way of writing code… especially when the vast majority of it is copied from other sources
http://www.w3schools.com/php/php_file_upload.asp
Thanks for sharing this I needed a upload function script example for an assessment 🙂
Nice, Thank you very much…
Pingback: Multiple File Upload With PHP and Dropzone.js- Infotuts
my problam is solved.thank you very much.
how to edit image which store in mysql using php
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.
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.
hello
Is there any idea how to implement the code in https://developers.google.com/maps/articles/phpsqlinfo_v3
That means, in order to be inserted into the application mapstore and options for storing image? OK, modify mysql database and add a column to store images, modify php code … but to modify html java code?
thanks
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”]));
to upload in a different folder with PHP for every individual user
https://eraazi.com/community/reply_forum.php?fid=22
thank’s for sharing useful tutorial
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
hi frd..its very nice..thanks…pls send image resize while uploading in php code in that above program connection
hi it script is also accepting video file correct it.
Yes! It’s working nice.. Thanks