如何使用php将jpg图像转换为适当的blob数据类型


How to convert jpg image to proper blob data type using php

<?php
$file_name = $_FILES['files']['name'];
$tmp_name  = $_FILES['files']['tmp_name'];
$file_size = $_FILES['files']['size'];
$file_type = $_FILES['files']['type'];
// The codes written above work fine and have proper information.
$fp = fopen($tmp_name, 'r'); // This one crashes.
$file_content = fread($fp, $file_size) or die("Error: cannot read file");
$file_content = mysql_real_escape_string($file_content) or die("Error: cannot read file");
fclose($fp);
....

我是PHP的新手。我试图将jpg图像作为blob存储在数据库中,但非常挣扎:(我尝试了许多教程和阅读文档,但仍然没有运气。有什么建议或教程可以帮助我吗?

使用fopen()打开二进制文件时,使用rb模式,即

$fp = fopen($tmp_name, 'rb');

或者,您可以简单地使用file_get_contents(),例如

$file_content = file_get_contents($tmp_name);

为了更好地报告错误,把它放在脚本的顶部

ini_set('display_errors', 'On');
error_reporting(E_ALL);