【发布时间】:2018-02-19 09:03:24
【问题描述】:
对于学校项目,我必须将图像保存在 mysql 数据库中,然后在网站上显示这些图像。为此,我使用以下代码保存图像:
$link = $link = new PDO('mysql:host=localhost;dbname=myDatabase', 'root', '');
$user = $_POST['user'];
$image = [];
$image = $_FILES['file'];
$image['string_scape'] = $link->quote(file_get_contents($_FILES['file']['tmp_name']));
$link->query('INSERT INTO userImage VALUES(NULL, "'.$image['string_scape'].'", "'.$image['type'].'", '.$image['size'].', '.$user.', NOW())') or die (print_r($link->errorInfo()));
$link = null;
这是我为图片制作的表格:
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| File | longblob | NO | | NULL | |
| Type | text | NO | | NULL | |
| Size | int(11) | NO | | NULL | |
| User | int(11) | NO | | NULL | |
| Date_added | datetime | NO | | NULL | |
+------------+----------+------+-----+---------+----------------+
为了显示图像,我使用以下代码:
//PHP file "show-image.php"
$link = new PDO('mysql:host=localhost;dbname=distribuidora', 'root', '');
$result = $link->query('SELECT * FROM userImage WHERE id = '.$_GET['id']) or die (print_r($link->errorInfo()));
$image = $result->fetch(PDO::FETCH_ASSOC);
header("Content-type:".$image["Type"]);
header("Content-Length:".$image["Size"]);
echo $image["File"];
HTML:
<img src="show-image.php?id=1" alt="image" />
这就是我去年的做法,它过去工作得很好,但从上周开始,我无法让图像显示出来。我在这里尝试了不同的标题和其他问题中给出的不同解决方案,但仍然无法正常工作。
【问题讨论】:
-
尝试在 Apache 中启用警告,这样我们就可以知道它产生了哪些警告/错误
-
您的 ID 字段是自动递增的。你不需要传递NULL
-
@MurtazaKhursheedHussain 警告已启用,但未显示任何错误。