我有一個問題:我想從我的iOS應用程序發送圖片到PHP腳本在MySQL數據庫中插入這一形象。圖像的mysql數據庫中的字段是LONGBLOB。正在發送的圖像是_photoImageView.image 我在Xcode中創建的方法如下:從iOS應用發送圖片PHP腳本MySQL數據庫
NSData *dataForImage = UIImagePNGRepresentation(_photoImageView.image);
NSMutableString *strURL = [NSMutableString stringWithFormat:@"http://localhost/myfff/join.php?username=%@&password=%@&photo=%@",_txtUsername.text,_txtPassword.text, dataForImage];
[strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strURL]];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
在患部的PHP腳本,我這樣做:
$data = addslashes(fread(fopen($_FILES[photo], "rb"))); $query = "INSERT INTO mytb VALUES (' ','$username','$password'','$data')";
插入不沒有發生..我錯在哪裏?請幫幫我!
您需要調試此一塊一塊的。作爲十首發,試圖使用'HTTP:// localhost'作爲目標網址中的iOS是行不通的,爲localhost實際上將手機本身。此外,將圖像編碼爲GET請求將不起作用,除非圖像非常非常小。 –
我在iphone模擬器上進行測試,所以它可以和localhost一起使用,事實上如果我運行沒有圖像部分的腳本,就會發生插入。 – Joker