2

我正在尝试使用 php 从我的 mysql 数据库中显示图像。图像未完全显示。它在尝试显示超过 200 kb 的图像时被剪切(由试验确定,但不太确定)。

HTML 代码:

<form enctype="multipart/form-data" action="insertimage.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="10240000" type="hidden">
<input name="image" accept="image/jpeg|image/jpg|image|JPG|image/png|image/gif" type="file">
<input value="Submit" type="submit">

PHP代码:

<?php
    require('myconnect.php');
    
    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 
    
              // Temporary file name stored on the server
              $tmpName  = $_FILES['image']['tmp_name'];  
               
              // Read the file 
              $fp     = fopen($tmpName, 'r');
              $data = fread($fp, filesize($tmpName));
              $data = addslashes($data);
              fclose($fp);
              
    
              // Create the query and insert
              // into our database.
              $query = "Update whyangry.posts set Photo='$data' where Pid=2";
              $results = mysql_query($query, $con);
              
              // Print results
              print "Thank you, your file has been uploaded.";
               
    $sql = "SELECT * FROM helpme.posts WHERE Pid=2";
    $res = mysql_query($sql,$con);
    while ($res1=mysql_fetch_assoc($res))
    {   
    $content = $res1['Photo'];
    $id=$res1['Pid'];
    
    }
    echo '<img src="data:image/png|image/jpeg|image/gif;base64,' . base64_encode( $content ) . '" />';
    echo 'Hello world.';
    
    }
    else {
       print "No image selected/uploaded";
    }
    
    ?>

在将 phpmyadmin 中的文件上传到 blob 数据类型时,我也收到以下错误

UPDATE `helpme`.`posts` SET `Photo` = 0xffd8ffe000104a46494600010201006000600000ffe10f074578696600004d4d002a0000000800060132000200000014000000564746000300000001000300004749000300000001003200009c9d00010000000e00000000ea1c0007000007f40000000087690004000000010000006a000000d4323030393a30333a31322031333a34373a34330000059003000200000014000000ac9004000200000014000000c0929100020000000335340000929200020000000335340000ea1c0007000007b40000000000000000323030383a30333a31342031333a35393a323600323030383a30333a31342031333a35393a3236000005010300030000000100060000011a00050000000100000116011b0005000000010000011e020100040000000100000126020200040000000100000dd90000000000000048000000010000004800000001ffd8ffe000104a46494600010100000100010000ffdb004300100b0c0e0c0a100e0d0e1211101318281a181616183123251d283a333d3c3933383740485c4e404457453738506d51575f626768673e4d71797064785c656763ffdb0043011112121815182f1a1a2f634238426363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363[...]

MySQL 说:

2006 - MySQL 服务器已经消失

请让我知道如何解决此问题。问题是在显示图像时。我不知道是否存在一些尺寸问题,请在这里提供帮助。

4

1 回答 1

1

使用addslashes与执行 SQL 查询的正确方法相去甚远。它并不总是能正确处理二进制数据。我不知道你在使用什么资源,但它教会了你非常坏的习惯。

不要 mysql_query在新应用中使用。这是 1990 年代遗留下来的界面,由于使用不正确会带来危险,该界面非常容易做到。最好mysqli在新项目中使用其中之一或 PDO。

您的查询应如下所示:

Update whyangry.posts set Photo=? where Pid=?

您可以在执行查询时绑定到这些占位符并避免出现编码问题。有很多关于如何正确执行此操作的示例。

于 2012-08-18T19:38:32.063 回答