douyi6960 2011-04-16 09:42
浏览 99
已采纳

iPhone上传图片到PHP文件的奇怪行为

Hey guys, I've been trying to upload pictures from my iPhone app to my server, but without concrete results. I once made it, but it seems like now it's not working anymore.

The code of my upload picture in the iPhone is the following:

- (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:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream

"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",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];
}

As you can see the code is pretty simple, I just set up a connection and I use a POST to send the thing. I use POST for posting the image, while the parameters are appended, so I use GET to get them.

On the PHP side the code is the following:

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);
}
}

As you can see from this code I just take the FILES variable and take out the extension, then I upload the file.

My problem is that nothing is uploaded and moreover the echo's that are throughout the code are not printed in the console of my iPhone simulator when I do "NSLog(returnString);".

Does anyone of you can get where the error is? I believe it's a stupid error, but you know, when keeping watching the same code for more than one hour then everything seems correct and you get frustrated even though the error is just under your eyes.

Thanks a lot.

  • 写回答

1条回答 默认 最新

  • douli1872 2011-04-22 19:19
    关注

    I found the solution. It was not in this piece of code I shown, but actually was a check done in the beginning of the file that was simply blocking a certain type of POSTs and GETs actions. It was of course not done by me, that's why I struggled with it so much. Anyhow thanks to everyone who attempted to solve my problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档