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
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.
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)
It depends on your usecases.
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
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
andwidth
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: