是否可以将这些文件上传到MySQL数据库?

时间:2015-07-21 22:49:52

标签: php mysql mysqli

在我的文件上传脚本中,我目前将所有文件都转到/ upload目录。我是否可以将其上传到数据库以及上传目录,以便我更容易管理?

<?php

require 'header.php';

?>

<center>
<?php

$uniqueid = uniqid();
$file = $uniqueid. basename( $_FILES["fileToUpload"]["name"]);
$target_dir = "uploads/$uniqueid";
$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 not an image.";
      //  $uploadOk = 1;
    }
}
?>
<br>
<?php
// Check if file already exists
if (file_exists($target_file)) {
    echo " Sorry, the file already exists or";
    $uploadOk = 0;
}
?>
<br>
<?php
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "txt") {
    echo "Sorry, only JPG, JPEG, PNG, GIF and TXT files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "your file was not uploaded correctly. Please try again or contact support!.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "Your file has been uploaded
        <br><br><br><br>
         <img src='uploads/$file'>";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>
</center>

1 个答案:

答案 0 :(得分:0)

简短回答:,这是可能的。

真实答案:你可能不应该。来自MySQL的服务文件比使用大多数Web服务器(如Apache,Nginx等)的文件系统提供静态文件慢5-10倍。