【问题标题】:How to convert jpg image to proper blob data type using php如何使用 php 将 jpg 图像转换为正确的 blob 数据类型
【发布时间】:2011-12-12 07:31:36
【问题描述】:
<?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 崩溃时,输出什么错误?尝试使用 E_ALL | E_STRICT 作为您的错误报告级别以获取更多信息。
  • 对不起,代码会是什么样的..?我不知道如何使用你的建议

标签: php blob fread


【解决方案1】:

使用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);

【讨论】:

  • 谢谢!!!! $file_content = file_get_contents($tmp_name);完全解决了。错误报告提示很棒。我从来没有用它来调试。 :D
猜你喜欢
  • 1970-01-01
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
  • 1970-01-01
相关资源
最近更新 更多