BLOB:在MySQL数据库中使用PHP存储图像

时间:2015-08-02 10:55:53

标签: php mysql html5 blob

当我打算深入挖掘时,我试图将图像存储到我的数据库中。我确实意识到不建议在数据库中存储图像并且被认为是“坏”但实际上,我认为它需要一个或多个专用服务器来实现文件数量限制。 PS。我对有关所有“在您的文件系统上存储图像然后通过MySQL引用其位置的工作”感到困惑。 到目前为止,这是代码,遗憾的是它会产生大量错误。

  

HTML

<form method = "POST" name = "sigup" onsubmit="return myFunction();" action "" ENCTYPE= "multipart/form-data">

<p><label class = "field">First Name:</label></p>
<input type = "text" name = "fname" class = "textbox-300" required>

<p><label class = "field">Upload Picture:</label></p>
<input type = "file" name = "imgfile"/>
<input type = "hidden" name = "check">
<input type = "submit" class = "button" name = "sub"  value = "Register">
</form>
  

PHP

<?php 
require ('config.php');

    if(isset($_REQUEST['check']) && $_FILES['imgfile'][size] > 0)
{
          $fileName       = $_FILES['imgfile'][name];         // image file name
          $tmpName     = $_FILES['imgfile'][tmp_name];       // name of the temporary stored file name
          $fileSize           = $_FILES['imgfile'][size];   // size of the uploaded file
          $fileType         = $_FILES['imgfile'][type];    // file type

          $fp                    = fopen($tmpName, 'r');  // open a file handle of the temporary file
          $imgContent  = fread($fp, filesize($tmpName)); // read the temp file
          fclose($fp); // close the file handle

          $query = "INSERT INTO image ('img_name', 'img_type', 'img_size', 'img_data')
                        VALUES ('$fileName', '$fileType', '$fileSize', '$imgContent')";

          mysql_query($query) or die('Error, query failed');
          $imgid = mysql_insert_id();                  // autoincrement id of the uploaded entry

          echo "<br>Image successfully uploaded to database<br>";
          echo "<a href=\'viewimage.php?id=$imgid\'>View Image</a>";

  else 
    die('You have not selected any image');


}
?>

2 个答案:

答案 0 :(得分:4)

如果在SQL查询中,statement.use返回ticks而不是单引号,则从页面底部移动结束花括号以关闭你。使用$ _FILES添加单引号,例如。 $ _FILES [ '值'] [ 'VAL'];     

<p><label class="field">First Name:</label></p>
<input type="text" name="fname" class="textbox-300" required>

<p><label class="field">Upload Picture:</label></p>
<input type="file" name="imgfile"/>
<input type="hidden" name="check">
<input type="submit" class="button" name="sub"  value="Register">
</form>
<?php 
require ('config.php');

    if(isset($_REQUEST['check']) && $_FILES['imgfile']['size'] > 0)
    {
          $fileName       = $_FILES['imgfile']['name'];         // image file name
          $tmpName     = $_FILES['imgfile']['tmp_name'];       // name of the temporary stored file name
          $fileSize           = $_FILES['imgfile']['size'];   // size of the uploaded file
          $fileType         = $_FILES['imgfile']['type'];    // file type

          $fp                    = fopen($tmpName, 'r');  // open a file handle of the temporary file
          $imgContent  = fread($fp, filesize($tmpName)); // read the temp file
          fclose($fp); // close the file handle

          $query = "INSERT INTO image (`img_name`, `img_type`, `img_size`, `img_data`)
                        VALUES ('$fileName', '$fileType', '$fileSize', '$imgContent')";

          mysql_query($query) or die('Error, query failed');
          $imgid = mysql_insert_id();                  // autoincrement id of the uploaded entry
          mysql_close($dbconn);

          echo "<br>Image successfully uploaded to database<br>";
          echo "<a href='viewimage.php?id=$imgid'>View Image</a>";
    }
  else 
    die('You have not selected any image');
?>

答案 1 :(得分:1)

答案是don't store images in a database ever

  

我对所有“在你的文件系统上存储图像,然后通过MySQL引用它的位置”的方式感到有点困惑

首先要解决你对这个问题的困惑。外部存储的文件系统用于存储图像。

将图像存储在一个文件夹中,最好通过网络服务器访问。

图像将从文档根目录存储为:

images/78_porsche.png

这就是应该存储在数据库中的内容。

然后,无论何时您需要工作或显示图像,您都知道它在哪里。

例如。

$image = "images/78_porsche.png";

<img src="<?= $image ?>" />
相关问题