duansao6776 2011-05-07 00:07
浏览 58
已采纳

无法将图像数据插入LongBlob字段

Total newbie here, please bear with me.

Building a very small personal app that uploads some images and info. I can't figure out why the following PHP/MySQL doesn't add the last insert in the query ($file_data) to my DB's longblob field.

All the other fields in the query insert fine, meaning I tested them one at a time, adding to the query, until I got to the last and then the insert fails. I am able to echo $file_data before the insert and see that the data is there, I've also found that hardcoding a string value for $file_data (i.e $file_data="this will insert") inserts fine... which is frustrating.

So my guesses are there's an error in the reading of the file ($fp fread etc) or that my longblob is setup wrong. File sizes are <16kb, so I'm sure it's not a php.ini issue either.

Any ideas? Thanks.

$boxtype=$_POST['type'];
$title=$_POST['title'];

if(isset($_POST['submit']) && $_FILES['imgfile']['size'] > 0)
{

    $filename = $_FILES['imgfile']['name'];
    $tmpName = $_FILES['imgfile']['tmp_name'];
    $file_size = $_FILES['imgfile']['size'];
    $mime_type = $_FILES['imgfile']['type'];        

    $fp = fopen($tmpName, 'r');
    $file_data = fread($fp, filesize($tmpName));
    fclose($fp);

    $query = "INSERT INTO table 
         (boxtype,title,filename,mime_type,file_size,file_data) 
         VALUES 
         ('$boxtype','$title','$filename','$mime_type','$file_size','$file_data')
         ";

    $db = db_connect(); 
    $result = $db->query($query) or die('Error, query failed');

    if ($result) 
    {
       echo "<br>Success<br>";
    }
}
else die("No Content");

MySQL Table:

CREATE TABLE `port` (
  `id` int(2) unsigned zerofill NOT NULL AUTO_INCREMENT,
  `boxtype` tinytext COLLATE latin1_general_ci NOT NULL,
  `title` varchar(40) CHARACTER SET latin1 NOT NULL,
  `filename` varchar(255) COLLATE latin1_general_ci NOT NULL,
  `mime_type` varchar(255) COLLATE latin1_general_ci NOT NULL,
  `file_size` int(11) NOT NULL,
  `file_data` longblob NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  • 写回答

4条回答 默认 最新

  • dongmeng4742 2011-05-07 00:13
    关注

    Because its a binary, it probably contains values which means its probably throwing a wobble

    Try using addslashes on the file_data variable, so it can save it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度