dtlygweb2017 2014-05-17 15:33
浏览 43
已采纳

$ _FILES始终为空

I am uploading a PDF to my server through my web-service, but $_FILES always seems to be empty regardless of what I do.

The program that will be uploading the PDF is an OSX program (via AFNetworking). I have ruled this out as a possibility of causing the issue as when I attempt to upload the file through 'CocoaRestClient' (similar to POSTMAN on chrome) I get nothing. In CocoaRestClient I am setting my ContentType to 'multipart/form-data'.

The PDF I am attempting to upload is 478kb so I do not think that size is the issue.

My PHP code is below. At the moment I am just trying to see if anything is in the $_FILES array at all.

<?php

header('Content-type: multipart/form-data');

$fileName = $_FILES['PDF']['name'];
$tmpName  = $_FILES['PDF']['tmp_name'];
$fileSize = $_FILES['PDF']['size'];
$fileType = $_FILES['PDF']['type'];

$response = array("filename" => $fileName,
                  "tmpName" => $tmpName,
                  "fileSize" => $fileSize,
                  "filetype" => $fileType,
                  "FILES" => $_FILES);

echo json_encode($response);

?>

This is the response I get from the JSON

{"filename":null,
 "tmpName":null,
 "fileSize":null,
 "filetype":null,
 "FILES":[]}

This is my objective-C code.

                [manager POST:pdfURL
                   parameters:nil
    constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
        [formData appendPartWithFileData:pdf name:@"PDF" fileName:filename mimeType:@"multipart/form-data"];
    }

                      success:^(NSURLSessionDataTask *operation, id responseObject) {
                        [self checkForErrorInResponse:responseObject];

                        [delegate pdfUploadedSuccessfullyWithResponse:responseObject];
                    }

                      failure:^(NSURLSessionDataTask *operation, NSError *error) {
                          [delegate pdfFailedToUploadWithError:error];
                      }];

After using POSTMAN to determine that my php is working correctly, I can conclude that my objective-C code is causing the bug. What is strange however, is that even though my php code returns null's, Charles is telling me that the PDF IS being uploaded!

  • 写回答

2条回答 默认 最新

  • doujin8476 2014-05-17 16:55
    关注

    Found the solution! The problem was that I wasn't setting the request serializers HTTP header field to 'application/pdf'.

    [manager.requestSerializer setValue:@"application/pdf" forHTTPHeaderField:@"Content-Type"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染