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

问题描述:

<?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存储在数据库中,但是却非常挣扎:(我尝试了许多教程并阅读了文档,但仍然没有运气.任何建议或教程都可能对我有所帮助..?

I'm a newbie to PHP stuff. I'm trying to store a jpg image as blob in a database but terribly struggling with it :( I tried many tutorials and read documents but still no luck. Any suggestions or tutorials that might help me out..?

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

When opening binary files with fopen(), use the rb mode, ie

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

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

Alternatively, you may simply use file_get_contents(), eg

$file_content = file_get_contents($tmp_name);

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

To enable better error reporting, place this at the top of your script

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