上传文件(图像)在实时服务器上不起作用,但在开发上起作用

Uploading a file (image) doesn't work on live server but works on dev

无法解决这个问题。以下代码适用于我的本地计算机,但在将网站上传到服务器后,我似乎没有收到请求中的文件。

这是我的 ajax:

dialogUploadPhoto.find('form').submit(function(event) {
    event.preventDefault();
    $.ajax({
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        data: $(this).serializeForm(), // method below...
        // data: formData,
        processData: false,
        contentType: false,
        cache: false,
        success: function(response) {
            // The response has the correct date on my local server
            dialogUploadPhoto.dialog( "close" );
        },
        error: function (xhr, desc, err){
            // On the live server, the debugger hits
        }
    });
});

serializeForm 方法只是一个 jquery 方法,用于将所有字段附加到 FormData 实例中:

$.fn.serializeForm = function() {
    var form = $(this),
        formData = new FormData();
    var formParams = form.serializeArray();

    $.each(form.find('input[type="file"]'), function(i, tag) {
        $.each($(tag)[0].files, function(i, file) {
            formData.append(tag.name, file);
        });
    });

    $.each(formParams, function(i, val) {
        formData.append(val.name, val.value);
    });

    return formData;
};

处理表单的 symfony 控制器方法如下所示:

/**
 * Matches /admin/upload/foto
 * @Route(
 *     "/upload/foto",
 *     options={"expose": true},
 *     name="admin_upload_foto")
 * @Method({"POST"})
 * @return JsonResponse
 */
public function upload_photo(
    Request $request
) {
    $response = new JsonResponse();

    $newPhoto = new Fotos();
    $photoForm = $this->createForm(PhotoType::class, $newPhoto);
    $photoForm->handleRequest($request);

    if ($photoForm->isSubmitted() && $photoForm->isValid()) {
        // This part is hit on the dev server
    } else if ($photoForm->isSubmitted()) {
        // This part is hit on the live server!
        // categorie contains no errors
        $photoForm['categorie']->getErrors()->__toString());
        // file contains an error: content-type is null;
        $photoForm['file']->getErrors()->__toString());
        $response->setStatusCode(400);
        $response->setData(array("result" => 0, "errors" => $errors));
    } else {
        $response->setStatusCode(400);
        $response->setData(array("result" => 0, "errors" => "Het foto-formulier is niet verzonden naar de server"));
    }
    return $response;
}

具体错误指出文件上传文件的 mime 类型为空。 这是否意味着文件永远不会发送? 如果我这样做: formData.getAll('file') 我可以看到文件实际上在包里。

在 Chrome 的网络分析器中,我可以看到有效负载确实包含该文件,也许它在途中丢失了?

根据要求 这是实时服务器上 $_FILES 的 var_dump

array(1) {
  ["photo"]=>
  array(5) {
    ["name"]=>
    array(1) {
      ["file"]=>
      string(8) "test.png"
    }
    ["type"]=>
    array(1) {
      ["file"]=>
      string(9) "image/png"
    }
    ["tmp_name"]=>
    array(1) {
      ["file"]=>
      string(14) "/tmp/phpwy5m9y"
    }
    ["error"]=>
    array(1) {
      ["file"]=>
      int(0)
    }
    ["size"]=>
    array(1) {
      ["file"]=>
      int(25745)
    }
  }
}

如有任何帮助,我们将不胜感激

找到了解决方案,但我不得不放弃自动 MIME 类型检查。

根据symfony documentation,我不应该关心文件是否存储为字符串。 mime 类型检查应该由 symfony 正确 'guessed' 但 mime 类型的断言规则不会选择上传文件的 mime 类型。

我删除了 mimetype 断言规则并使用手动扩展检查更改了我的处理方法如下:

    $newPhoto = new Fotos();
    $photoForm = $this->createForm(PhotoType::class, $newPhoto);
    $photoForm->handleRequest($request);
    //        var_dump($photoForm->getData());
    if ($photoForm->isSubmitted() && $photoForm->isValid()) {
        // $file stores the uploaded PDF file
        /* @var $file UploadedFile */
        $file = $newPhoto->getFile();
        $fileExtension = $file->guessClientExtension();
    //            var_dump($file->guessClientExtension());
        if (($fileExtension === 'png' || $fileExtension === 'jpeg' || $fileExtension === 'jpg')) {
           // Now I know the extension matches the type of files I want to handle
        } else {
           // Now I know the uploaded file is an invalid type
        }
    }