Laravel - UploadedFile 实例的文件路径

2022-01-08 00:00:00 upload php laravel laravel-4

我有一个 Laravel 4.2 API,它在创建资源时接受文件上传.该文件是用输入::file('file')

I have a Laravel 4.2 API that, when creating a resource, accepts file uploads. The file is retrieved with Input::file('file')

现在我想编写一个脚本(也在 Laravel 中),它将批量创建一些资源(所以我不能使用 POST 到 API 端点的 HTML 表单).如何将文件路径转换为 ​​UploadedFile 的实例,以便 Input::file('file') 将在 API 中获取它?

Now I want to write a script (also in Laravel) that will batch create some resources (so I can't use a HTML form that POSTs to API's endpoint). How can I translate a file path into an instance of UploadedFile so that Input::file('file') will pick it up in the API?

推荐答案

自己构造一个实例即可.API 是:

Just construct an instance yourself. The API is:

http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/File/UploadedFile.html

所以你应该可以做到:

$file = new UploadedFile(
    '/absolute/path/to/file',
    'original-name.gif',
    'image/gif',
    1234,
    null,
    TRUE
);

注意:您必须将第 6 个构造参数指定为 TRUE,以便 UploadedFile 类知道您正在通过单元测试环境上传图像.

Notice: You have to specify the 6th constructing parameter as TRUE, so the UploadedFile class knows that you're uploading the image via unit testing environment.

相关文章