【发布时间】:2019-04-08 14:45:37
【问题描述】:
我正在尝试将我的 HTML 表单的图像上传到我的 MySQL blob 列中。插入已成功完成,但知道直接插入 MySQL 的图像显示正确,图像的显示无法正常工作。
HTML 代码:
<form class="form-horizontal" method='POST' action="add_question_submit.php" id="addQuestionForm" enctype="multipart/form-data">
<div class="form-group">
<textarea rows="4" cols="50" name="question" form="addQuestionForm" placeholder="Enter Question in brief.... " required></textarea><br>
<input type="file" class="form-control" id="image" name="image" required><br>
<input type="text" class="form-control" id="answer" placeholder="Enter Correct Answer" name="answer" required><br>
<input type="number" class="form-control" id="category_id" placeholder="Enter category id (only numeric)" name="category_id" required><br>
<input type="number" class="form-control" id="level_id" placeholder="Enter level id (only numeric)" name="level_id" required><br>
<input type="submit" name="submit" value="submit" class="btn btn-primary">
</div>
</form>
PHP 代码:
$file_temp = base64_encode( file_get_contents( $_FILES['image']['tmp_name'] ) );
$image = getimagesize($file_temp);
$query = "INSERT INTO questions(question_name, image, answer, category_id,level_id)VALUES('$question', '$image','$answer', '$category_id', '$level_id')";
$result = mysqli_query($conn, $query);
header("Location: display_question.php");
display_question.php:
<td><?php echo '<img src="data:image/png;base64,'.base64_encode($row['image']).'" />'; ?></br><br/></td>
【问题讨论】:
-
在表格中存储图像是个坏主意
-
@SureshKamrushi,你有更好的主意吗?
-
你不是存储
getimagesize()的结果而不是图像吗? -
我已经尝试过存储变量 $file_temp 但没有任何反应。总是插入完成,显示不工作。
-
正如@Suresh Kamrushi 所说,最好将图像存储在文件系统的目录中,并将对图像的引用存储在数据库中。
标签: php mysql image file-upload base64