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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)