嘿家伙,我一直在试图从我的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