如何使用PHP将jpg图像转换为正确的blob数据类型

Rac*_*oon 2 php blob fread

<?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);

....
Run Code Online (Sandbox Code Playgroud)

我是PHP的新手.我正在尝试将jpg图像存储为数据库中的blob但是非常挣扎:(我尝试了很多教程并阅读了文档,但仍然没有运气.任何建议或教程可能会帮助我...?

Phi*_*hil 5

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

$fp = fopen($tmp_name, 'rb');
Run Code Online (Sandbox Code Playgroud)

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

$file_content = file_get_contents($tmp_name);
Run Code Online (Sandbox Code Playgroud)

要启用更好的错误报告,请将其放在脚本的顶部

ini_set('display_errors', 'On');
error_reporting(E_ALL);
Run Code Online (Sandbox Code Playgroud)