【问题标题】:Does it better to upload images in MySQL using PHP or just in server like firebase?使用 PHP 在 MySQL 中上传图像还是在像 firebase 这样的服务器中上传图像更好?
【发布时间】:2023-01-12 19:39:14
【问题描述】:

我仍然对此有疑问。所以我一直在考虑在 MySQL 中上传文件(如图像/文档/pdf 等)是否更好,但它说它会更慢......所以最好使用服务器端但我不知道它在哪里像火力基地?我也按照本教程在 mysql 中上传文件

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

这是这样的

<?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;
?>

还有关于服务器端......它是一个火力基地吗?或者它可能更多的东西对吗?

【问题讨论】:

    标签: php mysql server server-side


    【解决方案1】:

    它不会使 MySQL 变慢。如果你正确使用它并使用 PHP 7.x 或 8.x,你应该不会有任何问题。也有可能使用 AWS 来存储用户数据。

    【讨论】:

      【解决方案2】:

      将文件保存到任何文件持久性(本地文件系统或远程,如 AWS S3、Firebase 云存储等),并在数据库中仅保存该文件的路径(最好使用相对路径)

      【讨论】:

        猜你喜欢
        • 2019-03-12
        • 2012-09-29
        • 1970-01-01
        • 2018-08-05
        • 1970-01-01
        • 2019-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多