#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2017
    Posts
    4
    Rep Power
    0

    PHP Image Upload


    I'm building part of a solution that handles image uploads (in PHP) - and I was wondering which is a better place to upload images to?

    A database or in the web directory?
    And why?

    Thanks in advance...
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    Storing in a database lets you replicate across multiple servers but makes it a bit harder and slower to serve the resources to a client.
    Storing on a filesystem makes it easier to serve them on the source server but much harder for another server.

    So the answer depends on your server infrastructure.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2017
    Posts
    4
    Rep Power
    0
    Web directories are better than database as it has large space for storing your data.
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    Database servers have a lot of space too. That argument used to be valid 5-10 years ago, but now disk space isn't an issue.
  8. #5
  9. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,814
    Rep Power
    4536
    Another answer could be "outside the web directory", depending on your goals.
    -- Cigars, whiskey and wild, wild women. --
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2017
    Posts
    5
    Rep Power
    0
    I would vote for directory over database. You're going to increase overhead by storing a blob in a database.

    But as @Sepodati has stated, another option would be outside your web directory. If you are going to be allowing files to be uploaded to your web hosting account and you are going to make those uploaded files immediately web accessible, then you will have to sanitize your upload to keep from allowing malicious code to be uploaded and run through your web hosting space.

    This is one reason why uploading outside of your web accessible DocumentRoot is generally best.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2017
    Posts
    8
    Rep Power
    0
    You should store images in your database only if you have a specific need to, like security.

    Other than that, getting large files into databases usually isn't worth the trouble. Storing and retrieving the file get that much more complicated to implement, and database updates/upgrades/conversions have many more things that can go wrong.

    Like someone already replied here, this argument used to be valid 5-10 years ago, now storage space usn't an issue and you can upload images to the cloud. There are tools like Cloudinary that will help you with your PHP upload.

    They have a PHP library with available PHP methods for uploading images and other files to the cloud.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    464
    Rep Power
    44
    Hello Diana,

    As many have said... web space is not an issue.

    Your decision of storing images should based be on the ease of using it.

    If you auto generate photo name based on record id then I find that storing images in web folder is easier approach.

    Thx,
    Ranjan
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    2
    Rep Power
    0
    I believe the best practice is to store the images in the web directory and store its relative path in the DB, It is not the best practice to overload the database.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    1
    Rep Power
    0
    <?php
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    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;
    }
    }
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo