php (35) javascript (31) phpjavascript (30) jquery (23) html (20) mysql (14) database (9) codeigniter (4) json (4) bar chart (2) calendar (2) column chart (2) framework (2) google maps (2) query (2) tables (2) url (2) dropdown (1)

Wednesday, February 28, 2018

How to upload image using PHP?

Uploading images can be broken down into the three following steps.
  • An HTML form with a browse button to allow the client to choose which file to upload
  • A script to process the upload, validate the file, name it and place it in the file system
  • Lastly, a page to advise the client the upload was a success
Also, we are providing the solution to another most frequently asked question reviewing image before uploading into the folder by javascript.

HTML Code:

        <link rel="stylesheet" href="">
        <script src=""></script>
                position: absolute;
                width: 500px;
                height: 400px;
                top: 30%;
                left: 50%;
                margin: -100px 0 0 -150px;
                background: #80808061;
                color: white;
                width: 500px;
                margin-left: -15px;
                height: 40px;
                background: #464646;
                border-top-left-radius: 5px;
                border-top-right-radius: 5px;

        <div class="container">
            <div class="container-header">
                Select image to upload
            <form action="upload.php" method="post" enctype="multipart/form-data" id="form1" runat="server">
                <div class="col-md-12">
                    <input type='file' id="imgInp" name="fileToUpload" accept="image/*" onchange="loadFile(event)" class="form-control"/>
                    <img id="output"/>
                <div class="col-md-12">
                    <input type="submit" value="Upload Image" name="submit" class="btn">
        var loadFile = function (event) {
            var reader = new FileReader();
            reader.onload = function () {
                var output = document.getElementById('output');
                output.src = reader.result;

PHP Code:

$directory = "uploads/";
$target_file = $directory . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image using getimagesize function
if (isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if ($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;

// Check if file already exists by using file name
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;

// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
To access the file you uploaded, you need to store the path of the file in the database table which is very simple to do with basic knowledge.
$target_file is the path variable, you can store this variable value directly in the database by normal insertion query. 

Also, you need to create uploads folder in the root directory.


  1. Thanks you sharing how to file or img upload in php code. your post is very informative

  2. Hi! your blog is very nice. I read it completely with the deep eye. you have really done a great work. please keep it up.You have given such a great information that will be very useful for me.I want to know more information deeply about it. If possible you can share your more blog for us. Thank you for your such a great blog. great content I am also a content writer in best digital marketing company in udaipur which provide all services related to Digital Marketing in last five year we are recommended as a best digital marketing agency in udaipur by our client to other at last i want to express my thought really your blog is knowledge gaining. I really appreciate your blog.

  3. Buy perfect money in India @Rs.70/- per pm Sell @Rs.58/- per pm,CALL 08054019105, Chandigarh, 160017


    Buy/Sell Perfect Money At Reasonable Rates. We Provide Perfect Money At best Rates.
    We also sell buy Perfect Money, neteller and Bitcoin.

    Call us : +91 9517544444

    Gtalk : garrysingh1598


  4. All Assignment Help review
    A superior all assignment Help reviews offered by this website with the advantage of online support with high proficiency level based on its latest research and information by professional reviews writers. Wide ranges of subjects are covered with separate writers for each subject.

  5. Glance complicated in order to much added in pleasant from you!Lighting Warehouse Sydney

  6. We offer university assignment help fulfilling the specific and precise needs of students. Poor quality of an assignment drastically affects the score sheet of a student. Here at, the writers know how to proceed with an assignment writing task to make the end-result appreciable with world-class university assignment help online. We even welcome the feedback from the client’s end and try to sort out the shortcomings as soon as possible.

  7. Basically I'm targeting consumers who are looking at this condo new launch for sale to contact me via this website. I'm based in Singapore

    if you want more just look here "Martin place"

  8. Led lighting SydneyI actually take care of such information a lot. I'm interested in this specific info for just a while. Appreciate it and also all the best .

  9. ART Messenger is a WEB Browser messaging app FOR ALL DEVICES provided by Ask Real Teachers, which brings innovative communication technology to the fingertips of parents and students allowing them to connect with real teachers in Real-Time!

    if you want more just look here "tutoring"

  10. I've been looking for information on this topic for a while. I am happy this is great. Always do good
    duck life 3
    fnaf 3

  11. Great information to increase more effort on your code..thanks for sharing..
    mobile application development company in UK

  12. Papasiddhi is an offshore Mobile app development company. Our Team plans, innovative ideas for create mobile app and user-friendly for easy to use. We develop Hybrid app development, Android development, and Apple mobile application development. We work closely with clients to understand their needs, cost-effective, scale-able and powerful mobile solutions.

  13. Thanks For Sharing This, Very Interesting And Helpful Support Provide By this blog, This was the first news blog. Digital Signature Certificate

  14. Thank you so much for sharing these amazing tips. Digital Signature Certificate

  15. Thanks for sharing such an awesome post. This is very informative and helpful, share more Thank you so much for providing such useful content. Class 2 Digital Signature Certificate

  16. I really enjoyed reading your blog, you have lots of great content. Please visit here:
    Digital Signature Certificate in Delhi

  17. Nice information.. Hi dear, This is an nice and valuable post thanks for this information! GPS Tracking System in Punjab

  18. Good post. I study something more challenging on totally different blogs everyday. It'll always be stimulating to learn content from other writers and observe somewhat one thing from their store. I’d favor to use some with the content material on my blog whether you don’t mind. Natually I’ll offer you a hyperlink on your net blog. Thanks for sharing. men health screening singapore

  19. Search 1000s of job offers and find the best suitable position. Jobs near me from direct employers no agencies or jobs direct from top recruitment agencies & recruitment consultants. Send your job application to apply for a job with one click.

    if you want more just look here "jobs by skillsrecruitment"


More Posts