这是我将图像上传到 php 服务器的代码,但图像在服务器上作为损坏的图像上传。下面是我的代码,请告诉我问题..我已经尝试使用可可休息客户端,图像正在从中正确上传。
NSString *filename = @"connect-blue.png";
UIImage *image = [UIImage imageNamed:filename];
NSMutableURLRequest * request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"profile_image\"; filename=\"%@\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSData *imagedata = UIImageJPEGRepresentation(image,1.0f);
[postbody appendData:[NSData dataWithData:imagedata]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
php代码在这里
if(!empty($_FILES)){
//
$imgname=$_FILES['image']['name'];
$a=pathinfo($imgname);
if(!empty($a['extension'])){
$img_name=$a['filename'];
$img_ext=$a['extension'];
}
else
{
$img_name='';
$img_ext='';
}
$img_pre=mktime();
$image1=$img_pre."_".$imgname;
move_uploaded_file($_FILES['image']['tmp_name'], "uploads/images/" . $image1);