您好我需要通过BLOB将图像(用户的个人资料图片)存储到mySQL数据库,现在我知道更好的方法是将图像存储到服务器的文件系统并仅保存他们的URL到DB,但不是我的老板:),所以我把图像存储到DB!我有PHP代码,适用于android的java脚本(使用base64转换为二进制数据),我现在需要使用它为iPhone:这是我需要使用插入图像的PHP:
<?php
$base = $_REQUEST['image'];
$filename = $_REQUEST['filename'];
//connect to the db
$user = ‘root’;
$pswd = '';
$db = ‘test’;
$conn = mysql_connect(‘localhost’, $user, $pswd);
mysql_select_db('test');
$query = "INSERT INTO `test`.`photos` (`id`, `image`) VALUES (NULL,'$base')";
mysql_query($query) or die(mysql_error());
echo "Image id is ".mysql_insert_id();
echo $base;
?>
这是获取图片:
<?php
$id = $_REQUEST['id'];
$username = "root";
$password = "";
$host = "localhost";
$database = "test";
mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
mysql_select_db($database) or die("Can not select the database: ".mysql_error());
//select image
$query = "SELECT * FROM `photos` WHERE id =30 LIMIT 0 , 30";
$sql = mysql_query($query) or die(mysql_error());
while($image = mysql_fetch_array($sql)){
echo $image['image'];
}
?>
我正在尝试做这样的事情来存储图片:
//converting image to data end to binary data (base64)
NSData* data = UIImagePNGRepresentation(Image);
[Base64 initialize];
NSString *strEncoded = [Base64 encode:data];
//prepare request
NSString *strURL = @"My URL Here.";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:
[NSURL URLWithString:strURL]];
[request setDelegate:self];
[request setData:strEncoded withFileName:@"image.png" andContentType:@"image/png" forKey:@"image"];
[request startAsynchronous];
我在mySQL中看到它添加了新的BLOB,但没有我的图像([BLOB-0B])。 我丢失图像数据的任何想法?
答案 0 :(得分:0)
对PHP / Mysql
使用以下内容从DB(图像文件)中获取结果时,将内容存储到文件中......类似于
file_put_contents('User_photo.png',$ image ['image']);
如果您能够查看文件(作为图像),那么您的PHP代码就可以了......
选择查询必须仅适用于图像列(从照片中选择图像)
答案 1 :(得分:0)
我找到了回答我的问题:我只需要改变:$ base = $ _REQUEST ['image']; 到$ base = $ _POST ['image'];