【问题标题】:Problems with storing images in a mySQL database在 mySQL 数据库中存储图像的问题
【发布时间】:2012-01-30 20:45:01
【问题描述】:

** 已解决 **
我的代码有点问题

我必须使用 php 和 HTML 表单将图像存储到 mysql 数据库中。 我遵循了指南,但我并没有真正工作。它将文件上传到服务器......然后它被放弃......至少没有存储。

对于代码...文本和变量的语言是...荷兰语如果您对此部分有疑问,很乐意提供帮助。

目标表的数据库布局为:

Fotos

fotonr        int(10)                       Index number
album         varchar(20)                   Grouping catagory(not needed)
Lid           int(4)                        Index number of the member that placed it
type          varchar(20)                   To store what kind of image it is
image         blob                          The image itself

为此我使用以下段(数据库链接不在文件中,之前已经构建)

<p>
<?php
$target_path= "images/";
echo "checking file upload!<br />";
if(isset($_FILES['file']))
{
    echo"SET!!!<br />";
    if(isset($_POST['album']))
    {
        $album=trim($_POST['album']);
        if($album!="")
        {
            $album=  stripslashes($album);
        }
        else $album="Niet Ingedeeld/";
    }
    else $album="Niet Ingedeeld/";
    $myalbum=mysql_real_escape_string($album);
    $target_path=$target_path.$album;
    foreach($_FILES['file']['tmp_name'] as $key=>$afbeelding)
    {
        echo $_FILES['file']['name'][$key]."<br />";

        if ($_FILES['file']['tmp_name'][$key]!=''){
        $size = getimagesize($_FILES['file']['tmp_name'][$key]);
        $width=$size[0];
        $height=$size[1];
        echo "groote: ".$width."x".$height."<br />";
        if($width>800&&$height>600)
        {
            echo "Uw afbeelding is te groot!(maximaal 800x600)<br />";
        }
        else
        {

            $mynr= mysql_real_escape_string($_SESSION['nummer']);
            /*$type=exif_imagetype($_FILES['file']['tmp_name'][$key]);*/
            $type=$size[mime];
            echo 'Het type was:'.$type.'<br /> ';
            if($type=="image/gif" ||$type=="image/jpeg" ||$type=="image/bmp" ||$type=="image/png" ||$type=="image/gif" ){
            $mytype =mysql_real_escape_string($type);
            $tmpName  = $_FILES['file']['tmp_name'][$key];

            /*$fp      = fopen($tmpName, 'r');
            $content = fread($fp, filesize($tmpName));
            $mycontent = mysql_real_escape_string($content);*/
            $content = file_get_contents($tmpName); 
            $data = unpack("H*hex", $content ); 
            $mycontent = '0x'.$data['hex']; 


            $sql="INSERT INTO`nacverk`.`Foto` (`album` , `lid` ,`image`,`type` )VALUES ('$myalbum' ,'$mynr','$mycontent', '$mytype')";

            $result=mysql_query($sql); 
            /*fclose($fp);*/
            if(!$result)
            {
                echo "<h1>Bestand uploaden mislukt!<br /> ".mysql_error()."<br /></h1>";
            }
            else
            {
                echo "<h1>Bestand Succesvol geupload!<br /></h1>";
            }
            }
            else{
                echo "<h1> NOD32 detected a viral intrusion!<br /></h1>";
            }

        }

    }}
}
mysql_query("OPTIMIZETABLE `Foto` ");
?>
</p><hr />
<h3> Upload hier uw Foto's!</h3>
<hr />
<p>
<form enctype="multipart/form-data" action="" method="post" name="foto-upload">
<input name="MAX_FILE_SIZE" value="10485760" type="hidden">
Uw afbeeldingen:<br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
<input name="file[]"  type="file"><br />
Het album waar ze aan toegevoegd moeten worden:<br />
<input name="album" type="text" maxlength="20"
<?php if(isset($_GET['album']))echo ' value="'.$_GET['album'].'" '; ?>><br />
<input value="Submit" type="submit">
</form>
</p>
<hr />

直到需要上传到数据库的部分...
然后它触发 SQL 错误说:查询为空。

感谢您的宝贵时间!

【问题讨论】:

  • 通常最好将图像文件存储在 file 系统而不是数据库中。
  • @Dagon:一般来说,但并非总是如此。
  • @Bill 很棒的评论,那 not 适用于什么?
  • @Dagon:最好将图像存储在数据库中,例如,如果您需要图像服从事务隔离或回滚,或者在删除相应行时自动消失,或者包含在数据库备份中。

标签: php mysql sql image blob


【解决方案1】:

请记住,BLOB 数据类型的最大长度为 64KB。图像较大是很常见的,所以也许你应该使用 MEDIUMBLOB。

更多详情请见http://dev.mysql.com/doc/refman/5.1/en/string-type-overview.html

【讨论】:

    【解决方案2】:

    这是你的问题:

      $fp      = fopen($tmpName, 'r');
      $content = fread($fp, filesize($tmpName));
      $content = addslashes($content);
    

    您将文件内容解释为文本,而不是二进制数据。

    您必须以二进制格式将数据呈现给 mysql 服务器。不要添加斜杠。 (足够冗余?)

    (编辑)试试这个代码:

      $content = file_get_contents($tmpName);
      $data = unpack("H*hex", $content );
      $content = '0x'.data['hex'];
    

    【讨论】:

    • addlashes 来自我阅读的教程,mysql_real_escape_string($content) 会更好吗?
    • 是的,addlashes 不太好。你可以在这里阅读 PHP 安全专家 Chris Shiflett 的解释:shiflett.org/blog/2006/jan/…
    【解决方案3】:

    由于我现在无法对此进行测试,因此我不能 100% 确定,但我很确定 $_POST['file[]'] 和 $_FILES['file[]'] 不起作用。 $_POST['file'] 和 $_FILES['file'] 应该。我假设您在某处有输入,例如

    <input type="file" name="file[]" />
    

    这将允许您上传多个文件,这些文件应该存储在 $_FILES['file'] 中,然后它本身将是一个数组。 [] 表示它将是一个多值键,这意味着您需要在 PHP 站点中将其作为数组访问。

    根据我从您的帖子中了解到的情况,您正在学习这一点,所以请允许我建议您不要将图像存储在数据库中,除非您有充分的理由这样做。你不应该的原因有很多,我不想在这里讨论。您可以在 tizag.com http://www.tizag.com/phpT/fileupload.php 上找到关于文件上传的不错的老式教程。

    我希望这会有所帮助。祝你好运,享受:)

    【讨论】:

      【解决方案4】:

      您似乎缺少连接数据库的代码。

      $db = mysql_connect("localhost", "mysql_user", "mysql_password");
      

      生成的数据库句柄应作为第二个参数传递给mysql_query

      $result = mysql_query($query, $db); 
      

      为了方便排查,将MySQL错误信息写入网页:

      echo "<br>Error " . mysql_errno($db) . ": " . mysql_error($db) . "<br>";
      

      这是我之前用 a simple example of how to store an image in MySQL from PHP 写的答案。

      【讨论】:

      • 连接材料在其他部分处理。它还使用 $db_selected = mysql_select_db(...) 来选择正确的数据库。但我确实应该打印错误消息
      【解决方案5】:

      为什么不将参考信息存储在数据库中,并将文件存储在服务器上的文件夹中。

      例如

      fotonr        int(10)                       Index number
      album         varchar(20)                   Grouping catagory(not needed)
      Lid           int(4)                        Index number of the member that placed it
      type          varchar(20)                   To store what kind of image it is
      image         varchar(64)                   Path to The image itself
      

      这样,如果您需要删除图像,您知道图像的路径并且可以简单地取消链接,那么在没有 blob 的情况下访问图像和数据库上的查询会更快。

      【讨论】:

        猜你喜欢
        • 2011-03-30
        • 1970-01-01
        • 2011-05-07
        • 2011-06-21
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 2012-06-08
        • 2022-01-20
        相关资源
        最近更新 更多