在PHP中的文件上传在mysql [英] File upload in PHP in mysql

查看:111
本文介绍了在PHP中的文件上传在mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have searched some ways to download a file to mysql through a php form. This is what I found:

www .w3schools.com / php / php_file_upload.asp

但是这是上传到不同的页面而不是mysql。现在我改变代码到这一刻:

but this is for uploading to a different page instead of mysql. Now I changed the code to this at the moment:

    <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。
我做了google,但没有找到任何帮助我。
我的数据库如下所示:

I have no idea how to just upload it to mysql. I did google it but couldn't find anything which helped me. My database looks like this:

任何人都可以向我解释如何将我的文件上传到我的数据库表中。

Can anyone explain to me how to upload my file to my database table?

推荐答案

然而,如果你想在表中存储大数据,你需要创建单独的表只为文件数据,因为查询到大Blob数据的表非常慢。
例如带有列id(int)和数据(blob)的条形表'文件'
使用params和排序查询主表,然后通过id(to主表,您可以添加列file_id为例)

文件名:



However if you want store big data in table, you need create separate table only for file data because queries to table with big blob data very slow. For example crate table 'files' with columns id(int) and data(blob) And make query to main table with params and sorting and then give only data from table 'files' by id (to main table you may add column file_id for example) Filename:

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

这篇关于在PHP中的文件上传在mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆