4
我正在使用MYSQL
数据库将图像存储到我的iOS
应用程序中。我使用base64encoding
和解码技术来存储图像。 问题: 它正在将数据库中的名称存储为字符串,即可。但它不会将图像存储在htdocs文件夹中。我的问题在哪里以及如何解决?从iOS应用程序将图像存储到MYSQL数据库
PHP代码:
<?PHP
$host='localhost';
$name='root';
$pwd='';
$db='i';
$conn=mysql_connect($host,$name,$pwd);
mysql_select_db($db,$conn);
if($conn)
{ $image=$_POST['image_string'];
if($image!='')
{
$img = @imagecreatefromstring(base64_decode($image));
if($img != false)
{
imagejpeg($img, "htdocs/".$image."");}
}
$qur=mysql_query("INSERT INTO `j` (`id`, `name`) VALUES (NULL, '$image')");
if($qur)
{
echo "inserted";
}
else
{
echo mysql_error();
}
}
?>
客户端代码:
NSData *imageData=UIImagePNGRepresentation(_imageView.image);
NSString *string;
if([imageData respondsToSelector:@selector(base64EncodedStringWithOptions:)])
{
NSLog(@"iOS 7+");
string=[imageData base64EncodedStringWithOptions:kNilOptions];
}
else
{
string=[imageData base64Encoding];
}
NSString *post=[[NSString alloc]initWithFormat:@"image_string=%@",string];
post = [post stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
NSURL *url=[[NSURL alloc]initWithString:@"http://localhost/lastImage.php"];
NSData *postDAta=[post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postlength=[NSString stringWithFormat:@"%lu",(unsigned long)[postDAta length]];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postlength forHTTPHeaderField:@"Content-length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postDAta];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
我检查的许可和它只读。当我将它转换为读写权限时,它开始工作。谢谢@Mario。] –
如何在不使用'terminal'的情况下检查权限。第一步:右键点击文件夹。第二步:点击“获取信息”。第3步:检查共享和权限 –