1

所以我搜索了一些通过php表单将文件下载到mysql的方法。这是我发现的:

www.w3schools.com/php/php_file_upload.asp

但这是为了上传到不同的页面而不是 mysql。现在我现在将代码更改为:

    <form method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");


if(isset($_POST['submit'])) {
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if($_FILES["file"]["size"] < 20000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
  }
?> 

我不知道如何将其上传到mysql。我确实谷歌了它,但找不到任何对我有帮助的东西。我的数据库如下所示:

谁能向我解释如何将我的文件上传到我的数据库表?

4

1 回答 1

1

但是,如果要将大数据存储在表中,则需要仅为文件数据创建单独的表,因为对具有大 blob 数据的表的查询非常慢。例如 crate table 'files' with columns id(int) and data(blob) and make query to main table with params and sorts and then give only data from table 'files' by id (to main table you may add column file_id for示例)文件名:

<?php
// Allowed extension for upload
$allowedExts = array("gif", "jpeg", "jpg", "png");

$link = mysqli_connect("myhost", "myuser", "mypassw", "mybd") or die("Error " . mysqli_error($link));

if (isset($_POST['submit'])) {
    $filename = $_FILES["file"]["name"];
    // Get extension with http://www.php.net/manual/en/function.pathinfo.php
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    // Check file size in bytes and for allowed extensions
    if ($_FILES["file"]["size"] < 20000 && in_array($extension, $allowedExts)) {
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        } else {
            // Get file size
            $size = $_FILES["file"]["size"];
            // Read file to var
            $file_data = file_get_contents($_FILES["file"]["tmp_name"]);
            // Prepare sql
            $prepare = mysqli_prepare($link, 'INSERT INTO yourtable(`name`,`type`,`size`,`content`) VALUES(?,?,?,?)');
            // bind variables for replace ? in query with types
            // s - string, i - integer, b- blob (for file data), d - for double
            mysqli_stmt_bind_param($prepare, 'ssib', $filename, $extension, $size, $file_data);
            // execute query
            mysqli_stmt_execute($prepare);
        }
    } else {
        echo "Invalid file";
    }
}
?>
于 2013-10-14T11:40:49.460 回答