如何使用 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 存储在数据库中,但非常挣扎:(我尝试了很多教程并阅读了文档,但仍然没有运气。有什么建议或教程可以帮助我......?
<?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);
....
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..?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
fopen()
打开二进制文件时,请使用rb
模式,即或者,您可以简单地使用
file_get_contents()
,例如为了更好地启用错误报告,将其放在脚本的顶部
When opening binary files with
fopen()
, use therb
mode, ieAlternatively, you may simply use
file_get_contents()
, egTo enable better error reporting, place this at the top of your script