skip to Main Content

I still have a question in my mind. So I’ve been wondering if it’s better to upload a file (like images/docs/pdf, etc.) in MySQL but it said it would be slower…so it better to used a server-side but I don’t know where is it like firebase? Also, I follow this tutorial to upload files in MySQL

https://www.codexworld.com/upload-store-image-file-in-database-using-php-mysql/

which is something like this

<?php
// Include the database configuration file
include 'dbConfig.php';
$statusMsg = '';

// File upload path
$targetDir = "uploads/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
    // Allow certain file formats
    $allowTypes = array('jpg','png','jpeg','gif','pdf');
    if(in_array($fileType, $allowTypes)){
        // Upload file to server
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
            // Insert image file name into database
            $insert = $db->query("INSERT into images (file_name, uploaded_on) VALUES ('".$fileName."', NOW())");
            if($insert){
                $statusMsg = "The file ".$fileName. " has been uploaded successfully.";
            }else{
                $statusMsg = "File upload failed, please try again.";
            } 
        }else{
            $statusMsg = "Sorry, there was an error uploading your file.";
        }
    }else{
        $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
    }
}else{
    $statusMsg = 'Please select a file to upload.';
}

// Display status message
echo $statusMsg;
?>

And also about the server-side..Is it a firebase? or it could be something else right?

4

Answers


  1. It does not make MySQL slower.. If you use it correctly and use PHP 7.x or 8.x, you should not have any problems. There is also a possibility to use AWS to store user data.

    Login or Signup to reply.
  2. Save file into any files persistence (local file system or remote, like AWS S3, Firebase Cloud Storage, etc.) and in database save only path to that file (best use relative path)

    Login or Signup to reply.
  3. It depends on your usecases.

    • Do you need direct access to the uploaded file? If so, database blob storage might not be the best solution.
    • Can you keep your database and the storage "in sync"? If you are going to remove files from the filesystem, your database records should be deleted too somehow.
    • What’s the volume of the amount and size of the files? Can you afford that in terms of database costs (financial and otherwise) vs. a probably less cost affective filesystem.
    Login or Signup to reply.
  4. For images (etc) that will be used on a web page, I prefer to store the image in a file and store the URL to that file in the database. Then, in HTML, use that path in

    <img src=...jpg height=.. width=..>
    

    That makes loading the page arguably simpler and faster. (HTML handles opening a separate connection to download the image later in the processing.)

    I also like to add height and width to the image tag so that the screen does not bounce around before it downloads the image. If you are linking off to, say, a pdf reader, the dimensions are not needed:

    <a href=...pdf>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search