BLOB: Storing Images using PHP inside MySQL Database [duplicate]

放肆的年华 提交于 2019-12-12 00:22:31

问题


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

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


}
?>

回答1:


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



回答2:


The answer is don't store images in a database ever

I'm a little confused about how all the "storing images on your filesystem and then referencing its location via MySQL works"

Rather handle your confusion on this issue first. The file system of external storage are designed to store images.

Store the images in a folder, preferably accessible via your web server.

The images will be stored from the document root as:

images/78_porsche.png

So that is what should be stored in the database.

Then whenever you need to do work or display the image, you know where it is.

eg.

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

<img src="<?= $image ?>" />


来源:https://stackoverflow.com/questions/31771139/blob-storing-images-using-php-inside-mysql-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!