Home > front end >  Does it better to upload images in MySQL using PHP or just in server like firebase?
Does it better to upload images in MySQL using PHP or just in server like firebase?

Time:01-13

I still have question this in my mind. So I've been thinking if its better to upload a file (like images/docs/pdf and etc) in MySQL but it said it will 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 file 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 server-side..Is it a firebase? or it could be more something else right?

CodePudding user response:

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)

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

Database only stores your file name, and the actual file sis stored on your server.

  • Related