Symfony2 从外部程序上传文件时遇到问题 - 'mimeType' => 'application/octet-stream'

Having trouble with Symfony2 uploading file from external program - 'mimeType' => 'application/octet-stream'

我有一个 Symfony2 端点需要接受一个 post 请求,其中包含一些文本参数和一个由第 3 方程序提供的 zip 文件。为了快速开发,我开始使用 Chrome POST 插件来欺骗发送表单数据。使用该方法时,我能够很好地检索文件。

但是,当我切换到第 3 方 Windows 应用程序上传报告时,我无法再检索文件。我现在没有弄清楚它是一个 zip 文件,而是 mimeType' => 'application/octet-stream',文件大小为 0.

我在下面的 link 中读到,我需要确保我的 post_max_size 和 upload_max_filesize 足够大以处理该文件。 Can't upload image with mime-type "application/octet-stream" in Symfony2

我不想乱来,所以我把它设置为 100mb,但还是不行。

这是请求的转储:

[2016-08-24 10:15:48] app.INFO: Request dump: Symfony\Component\HttpFoundation\Request::__set_state(array(    'attributes' =>    Symfony\Component\HttpFoundation\ParameterBag::__set_state(array(      'parameters' =>      array (       '_format' => 'xml',       '_controller' => 'Company\Bundle\CompanyBundle\Controller\DefaultController::cloudTransferReport',       '_route' => 'hg_cloudtransfer_report',       '_route_params' =>        array (         '_format' => 'xml',       ),       '_method' =>        Sensio\Bundle\FrameworkExtraBundle\Configuration\Method::__set_state(array(          'methods' =>          array (           0 => 'POST',           1 => 'GET',         ),       )),     ),   )),    'request' =>    Symfony\Component\HttpFoundation\ParameterBag::__set_state(array(      'parameters' =>      array (       'submitter' => 'hgis',       'version' => '5.1.26.0',       'userName' => 'admin',       'password' => 'kitten',     ),   )),    'query' =>    Symfony\Component\HttpFoundation\ParameterBag::__set_state(array(      'parameters' =>      array (     ),   )),    'server' =>    Symfony\Component\HttpFoundation\ServerBag::__set_state(array(      'parameters' =>      array (       'REDIRECT_HTTPS' => 'on',       'REDIRECT_SSL_TLS_SNI' => 'example.com',       'REDIRECT_STATUS' => '200',       'HTTPS' => 'on',       'SSL_TLS_SNI' => 'example.com',       'CONTENT_TYPE' => 'multipart/form-data, boundary=AaBbCcDdHhGg40',       'HTTP_USER_AGENT' => 'Some software v 5.1.26.0',       'HTTP_HOST' => 'example.com',       'CONTENT_LENGTH' => '3592731',       'HTTP_CACHE_CONTROL' => 'no-cache',       'PATH' => '/usr/bin:/bin:/usr/sbin:/sbin',       'SERVER_SIGNATURE' => '',       'SERVER_SOFTWARE' => 'Apache/2.4.18 (Unix) PHP/5.5.36 LibreSSL/2.2.7',       'SERVER_NAME' => 'example.com',       'SERVER_ADDR' => '192.168.1.100',       'SERVER_PORT' => '443',       'REMOTE_ADDR' => '192.168.1.223',       'DOCUMENT_ROOT' => '/Users/user/Documents/symphony/cpsrecall/web',       'REQUEST_SCHEME' => 'https',       'CONTEXT_PREFIX' => '',       'CONTEXT_DOCUMENT_ROOT' => '/Users/user/Documents/symphony/cpsrecall/web',       'SERVER_ADMIN' => 'chris@yourcontactpoint.com',       'SCRIPT_FILENAME' => '/Users/user/Documents/symphony/cpsrecall/web/app.php',       'REMOTE_PORT' => '51248',       'REDIRECT_URL' => '/hg/cloudtransfer/upload-report',       'GATEWAY_INTERFACE' => 'CGI/1.1',       'SERVER_PROTOCOL' => 'HTTP/1.1',       'REQUEST_METHOD' => 'POST',       'QUERY_STRING' => '',       'REQUEST_URI' => '/hg/cloudtransfer/upload-report',       'SCRIPT_NAME' => '/app.php',       'PHP_SELF' => '/app.php',       'REQUEST_TIME_FLOAT' => 1472055346.6129999,       'REQUEST_TIME' => 1472055346,     ),   )),    'files' =>    Symfony\Component\HttpFoundation\FileBag::__set_state(array(      'parameters' =>      array (       'file1' =>        Symfony\Component\HttpFoundation\File\UploadedFile::__set_state(array(          'test' => false,          'originalName' => '00000003-1.zip',          'mimeType' => 'application/octet-stream',          'size' => 0,          'error' => 1,       )),     ),   )),    'cookies' =>    Symfony\Component\HttpFoundation\ParameterBag::__set_state(array(      'parameters' =>      array (     ),   )),    'headers' =>    Symfony\Component\HttpFoundation\HeaderBag::__set_state(array(      'headers' =>      array (       'content-type' =>        array (         0 => 'multipart/form-data, boundary=AaBbCcDdHhGg40',       ),       'user-agent' =>        array (         0 => 'Some software v 5.1.26.0',       ),       'host' =>        array (         0 => 'example.com',       ),       'content-length' =>        array (         0 => '3592731',       ),       'cache-control' =>        array (         0 => 'no-cache',       ),       'x-php-ob-level' =>        array (         0 => 1,       ),     ),      'cacheControl' =>      array (       'no-cache' => true,     ),   )),    'content' => '',    'languages' => NULL,    'charsets' => NULL,    'encodings' => NULL,    'acceptableContentTypes' => NULL,    'pathInfo' => '/hg/cloudtransfer/upload-report',    'requestUri' => '/hg/cloudtransfer/upload-report',    'baseUrl' => '',    'basePath' => NULL,    'method' => 'POST',    'format' => NULL,    'session' =>    Symfony\Component\HttpFoundation\Session\Session::__set_state(array(      'storage' =>      Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage::__set_state(array(        'bags' =>        array (         'attributes' =>          Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag::__set_state(array(            'name' => 'attributes',            'storageKey' => '_sf2_attributes',            'attributes' =>            array (           ),         )),         'flashes' =>          Symfony\Component\HttpFoundation\Session\Flash\FlashBag::__set_state(array(            'name' => 'flashes',            'flashes' =>            array (           ),            'storageKey' => '_sf2_flashes',         )),       ),        'started' => false,        'closed' => false,        'saveHandler' =>        Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy::__set_state(array(          'handler' =>          SessionHandler::__set_state(array(         )),          'wrapper' => true,          'active' => false,          'saveHandlerName' => 'files',       )),        'metadataBag' =>        Symfony\Component\HttpFoundation\Session\Storage\MetadataBag::__set_state(array(          'name' => '__metadata',          'storageKey' => '_sf2_meta',          'meta' =>          array (           'c' => 0,           'u' => 0,           'l' => 0,         ),          'lastUsed' => NULL,          'updateThreshold' => '0',       )),     )),      'flashName' => 'flashes',      'attributeName' => 'attributes',   )),    'locale' => NULL,    'defaultLocale' => 'en', )) [] []

对于那些不想阅读那么长的行的人,这里只是 FileBag:

[2016-08-24 10:15:48] app.INFO: file1dump: Symfony\Component\HttpFoundation\File\UploadedFile::__set_state(array(    'test' => false,    'originalName' => '00000003-1.zip',    'mimeType' => 'application/octet-stream',    'size' => 0,    'error' => 1, )) [] []

这是我应该抓取文件的代码,但也许我做错了:

public function cloudTransferReport(Request $request)
{
    $logger = $this->get('logger');
    $em = $this->getDoctrine()->getManager();

    $data = $request->request;
    $files = $request->files;

    $submitter = $data->get('submitter');
    $version = $data->get('version');
    $memberName = $data->get('userName');
    $password = $data->get('password');
    $file1 = $files->get('file1');

    $logger->info("file string: " . var_export($stuff, true));
    $logger->info("file1dump: " . var_export($file1, true));

如果您需要该控制器的更多代码,请告诉我,但我认为此时我至少应该能够将文件保存在内存中。

好的 Mac 运行 遇到这样问题的用户,尤其是那些使用过自制软件的用户,可能安装了也可能没有安装多个 PHP 版本。

您的计算机上可能有多个 php.ini 文件。我运行 php --ini,结果是Loaded Configuration File: /usr/local/etc/php/5.6/php.ini。错了!

最终 Apache 使用的真正 php.ini 文件在 /etc/php.ini 中,所以请不要浪费一整天的时间来敲击键盘。确保您在 Apache 实际使用的文件中!就这些。我现在要爬到桌子底下哭了。