Laravel-UploadedFile实例的文件路径 [英] Laravel - file path to UploadedFile instance
问题描述
我有一个Laravel 4.2 API,当创建资源时,它接受文件上传.使用以下方式检索文件
Input::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/组件/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.
这篇关于Laravel-UploadedFile实例的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!