Upload File Using PHP - Example For Image

Create The HTML Form

Create an HTML form, so you can choose file want to upload:

***********************

Code

***********************

<form action="upload.php" method="post" enctype="multipart/form-data">
  Please choose a file:
<input type="file" name="uploadFile" accept="image/*" />
<br>
  <input type="submit" value="Upload File" />
</form>

***********************

End of Code

***********************

On the Above code accept is use for accepting only Image file as a File Input. If no accept is specify, All File are accepted.


Create a upload.php file , whose code will be as Follow:

***********************

Code

***********************

?php
$target_dir = "uploads/";
$target_dir = $target_dir . basename( $_FILES["uploadFile"]["name"]);
$uploadOk=1;

// Check if file already exists
if (file_exists($target_dir . $_FILES["uploadFile"]["name"])) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}

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

// Only GIF files allowed 
if (!($uploadFile_type == "image/gif")) {
    echo "Sorry, only 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["uploadFile"]["tmp_name"], $target_dir)) {
        echo "The file ". basename( $_FILES["uploadFile"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

***********************

End of Code

***********************



IF You want to Know about, How to make Thumbnail of the Uploaded file using PHP, Click Here





Comments

  1. You did well on inspiring writers like me. Please keep on doing such content.
    PHP Training Center in Chennai
    PHP Course Chennai

    ReplyDelete
  2. I was very pleased to find this site.I want to thank you for this great content!! I enjoyed every little bit of it and I have you bookmarked to check out new stuff you post.
    Loadrunner training institute in Chennai | Qtp course in Chennai

    ReplyDelete
  3. I found your post while searching for some related information on blog search... Its a great blog, keep posting and update the information.
    French Course in Chennai
    French Language Classes in Chennai
    best german classes in chennai

    ReplyDelete
  4. It's extraordinarily profitable site for learn. This present information are to a great degree valuable to us. It will improve my knowledge. Grateful to you for sharing this superb site.
    Article Submission sites | Latest Updates | Technology

    ReplyDelete
  5. Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
    digital marketing course in coimbatore
    php training in coimbatore

    ReplyDelete
  6. Thanks for providing wonderful information with us. Thank you so much.
    Regards,
    PHP Training in Chennai | PHP Course in Chennai

    ReplyDelete
  7. Good resources.
    Thanks for sharing
    Java Tutorials | Learn Java

    Well organized and easy to understand programming tutorials with lots of examples of why,what and how to program in Java and OOPs

    ReplyDelete

Post a Comment

Popular posts from this blog

Page Unload Show Notification to user

Function Oriented File Upload PHP