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条)

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写