duanjiong5686 2014-10-07 15:15
浏览 100
已采纳

iOS到PHP图片上传

I am trying to upload an image to my server from IOS7 via php5.

iOS Code

NSString *urlString = @"http://<domain>/conAPI/imageAPI.php";
NSData *imageData = UIImagePNGRepresentation(venueImageView.image);

NSMutableURLRequest *requestImage = [[NSMutableURLRequest alloc] init];
    [requestImage setURL:[NSURL URLWithString:urlString]];
    [requestImage setHTTPMethod:@"POST"];

NSString *boundary = @"0xKhTmLbOuNdArY";

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[requestImage addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedimage\"; filename=\"V%@.png\"
",[userDictioanry objectForKey:@"user_id"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[requestImage setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:requestImage returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"Image Return String: %@", returnString);

and my php

ini_set('log_errors',1);
$uploaddir = '/var/www/html/memberImages/';
print_r($_FILES);
$file = $_FILES['uploadedimage']['name'];
$uploadfile = $uploaddir . $file;

if (file_exists($uploaddir) && is_writable($uploaddir)) {
    if (move_uploaded_file($_FILES['uploadedimage']['tmp_name'], $uploadfile)) {
       echo 'DONE '.$file;
    }else {
        echo "error ".$_FILES['uploadedimage']['tmp_name'].' '.$uploadfile;
    }
}else {
    if (!file_exists($uploaddir)){
        echo 'upload dir does not exist '.getcwd().' '.$uploadfile.'
';
    }
    if(!is_writable($uploaddir)) {
        echo 'Upload directory is not writable.'.'
';
    }
}

Under all circumstances I get "error" returned with the two filenames. error.log shows nothing. root user has read/write to the directory and all parents.

Can you please point out the simple flaw in my logic. I would also appreciate it if this did not involve third party methods. There are valid reasons for not using AFNetworking in this case.

Thanks in advance Chris

  • 写回答

2条回答 默认 最新

  • drjk87189 2014-10-07 16:36
    关注

    This was NOT a problem with the code.....

    The directory membersImages was somehow corrupt. permissions showed 776 and owner showed as root user, root user could CD into the directory, but a LS showed zero content. exiting and rmdir failed because directory was not empty. recursive delete worked, MKDIR and upload works.

    Color me bizarred.....

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题