BLOB:在MySQL数据库中使用PHP存储图像 [英] BLOB: Storing Images using PHP inside MySQL Database

查看:79
本文介绍了BLOB:在MySQL数据库中使用PHP存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打算深入研究时,在这里我试图将图像存储到我的数据库中.我确实意识到不建议将图像存储在数据库中,并且被认为是不好的",但是实际上,我认为它需要一台或多台专用服务器来限制文件数. 附言我对所有在文件系统上存储图像,然后通过MySQL引用其位置"的工作方式感到困惑. 到目前为止,这里是代码,可悲的是它会产生大量错误.

As I plan to dig deep, here I am trying to store images into my database. I do realize that storing images in the database is not recommended and is considered "bad" but practically, I think it requires one or more dedicated servers for file count limit. Ps. I'm a little confused about how all the "storing images on your filesystem and then referencing its location via MySQL works". Here is the code so far, sadly it produces a large number of errors.

HTML

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

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


}
?>

推荐答案

如果您使用if语句,请从页面底部移动右大括号以使其关闭.请在SQL查询中使用反引号代替列名的单引号.在使用$ _FILES的地方添加单引号,例如. $ _FILES ['value'] ['val'];

Move the closing curly brace from the bottom of the page to close you if statement.use back ticks instead of single quotes for column names in you sql query. Add single quotes where using $_FILES eg. $_FILES['value']['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');
?>

这篇关于BLOB:在MySQL数据库中使用PHP存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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