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

Deadly 提交于 2019-12-10 10:48:00

问题


<?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..?


回答1:


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

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

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


来源:https://stackoverflow.com/questions/7845233/how-to-convert-jpg-image-to-proper-blob-data-type-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!