Search code examples
phpobjective-cpdfafnetworking-2uploading

$_FILES is always empty


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!


Solution

  • 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"];