【问题标题】:AFNetworking upload imageAFNetworking上传图片
【发布时间】:2014-04-15 19:19:43
【问题描述】:

我正在尝试从我的 iOS 应用程序将图像上传到我的服务器,但 php 脚本中的“不成功”一直在欺骗。我做错了什么?

Log 返回“File = Unsuccessful”,然后是二进制文件编号。

iOS(上传图片):

-(IBAction)uploadPhoto:(id)sender{

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://SERVERURL"]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSData *imageData = UIImageJPEGRepresentation(pickedImage, 0.5);
    NSDictionary *parameters = @{@"message": self.descriptionView.text};
    AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //do not put image inside parameters dictionary as I did, but append it!
        [formData appendPartWithFileData:imageData name:@"file" fileName:@"upload.jpg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@ ***** %@", operation.responseString, error);
   }];
    [op start];


        [self dismissViewControllerAnimated:YES completion:nil];
    } 
}

PHP 代码:

<?
if(!empty($_POST)) 
{
    $message = $_POST['message'];
    $directory = $_SERVER['DOCUMENT_ROOT'] . '/pictures';
    $file = basename($_FILES['userfile']['upload.jpg']);
    $uploadfile = $directory . $file;
    var_dump($_FILES);
    $randomPhotoID = md5(rand() * time());
echo 'file='.$file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
       echo 'successful';

    }
    else
    {
    echo 'unsuccessful';

}
}
else
{
    echo('Empty post data');
}
?>

【问题讨论】:

    标签: php ios objective-c afnetworking


    【解决方案1】:

    不要忘记目录末尾的 /,因为您将其与文件名连接起来:

    $directory = $_SERVER['DOCUMENT_ROOT'] . '/pictures/';
    

    如果您仍然遇到问题,请尝试添加更多检查:

    <?php
    if(isset($_POST)) {
      $message = $_POST['message'];
      if(is_uploaded_file($_FILES['userfile']['tmp_name']){
        //we got something, set it up
        $directory = $_SERVER['DOCUMENT_ROOT'] . '/pictures/';
        $file = basename( $_FILES['userfile']['name']);        
        $uploadfile = $directory . $file; 
        $randomPhotoID = md5(rand() * time());
        //perform the upload
      if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
          echo 'successful';
        } else {
          echo 'unsuccessful';
        }
      }else{
          echo "Nothing was uploaded";
      }
    }else{
        echo 'POST is not set cannot proceed to upload';
    }
    

    【讨论】:

    • 你可以尝试硬编码目录路径
    • 我已经对其进行了硬编码,并在目录路径中发现了一个小错误,我现在确定它是正确的,但并没有解决问题。服务器是ubuntu服务器,不知道会不会有问题。
    • @user3423384 那么脚本回显了什么?你没有提到那部分
    • var/www/User/core/ios/pictures/
    猜你喜欢
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    相关资源
    最近更新 更多