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步:檢查共享和權限 –