1
這裏是我的代碼上傳圖像到php服務器,但圖像上傳爲服務器上的損壞的圖像。下面是我的代碼,請告訴我的問題..我嘗試了可可休息客戶端,圖像正在從它正確上傳。當從iPhone發佈到服務器時,圖像被損壞
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);
還發布PHP代碼 – GBD
@GBD支票p hp code – iProgrammer
你可以檢查'var_dump($ _ FILES)的輸出;'並將其粘貼到你的問題中 – GBD