嘿傢伙,我一直在試圖從我的iPhone應用上傳圖片到我的服務器,但沒有具體的結果。我曾經做過,但現在看起來它不再工作了。iPhone上傳圖片到PHP文件奇怪的行爲
在iPhone我上傳圖片的代碼如下:
- (void) uploadPicture: (UIImageView *) newImage withParams: (NSString *) params{
//newImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://farm3.static.flickr.com/2662/4149796861_80391f5364.jpg"]]];
NSData *imageData = UIImageJPEGRepresentation(newImage.image, 90);
NSString *urlString = [@"http://my.server/mobile_interface.php?" stringByAppendingString: params];
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
*/
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
[self setImageTaken: nil];
}
正如你所看到的代碼非常簡單,我只是成立了一個連接,我使用POST發送的東西。我使用POST發佈圖像,而附加參數,所以我使用GET來獲取它們。
在PHP端的代碼如下:
if(isset($_GET["action"]) && $_GET["action"] == "upload_picture_new_user" && isset($_GET["user_id"])){
$uploaddir = '/my/path/';
echo $uploaddir;
$image = $_FILES['userfile']['name'];
$file = basename($_FILES['userfile']['name']);
//find extension of the file
$ext = findexts($file);
echo $file;
echo $ext;
$ran = rand();
$file = $ran;
$filename = $file .'.'. $ext;
echo $filename;
$uploadfile = $uploaddir . $file .'.'. $ext;
/*$filePrev = $file.'_prev';
$uploadprev = $uploaddir . $filePrev .'.'. $ext; */
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "uploaded_images/".$filename;
$query = "INSERT INTO `my_table` (`profilepic`) VALUES ('uploaded_images/".$filename."')";
//do the query
do_query($query);
}
}
正如你可以從這個代碼中看到我只是採取FILES變量,並採取了延長的話,我上傳的文件。
我的問題是沒有上傳任何東西,而且當我做「NSLog(returnString);」時,遍及代碼的echo不會打印在我的iPhone模擬器的控制檯中。
有沒有人可以得到錯誤的地方?我相信這是一個愚蠢的錯誤,但是你知道,當持續觀看相同的代碼超過一個小時時,一切似乎都是正確的,即使錯誤只在你的眼睛下面,你也會感到沮喪。
非常感謝。
是否定義了'findexts()?如果你打開錯誤報告,會發生什麼? – 2011-04-16 09:44:12
是的,它已定義,並且錯誤報告已打開。 – Masiar 2011-04-16 09:51:27
奇怪。如果在PHP代碼中執行'print_r($ _ FILES);'和'print_r($ _ POST)'會發生什麼? – 2011-04-16 09:53:40