沃梦达 / 编程问答 / php问题 / 正文

Laravel - UploadedFile 实例的文件路径

Laravel - file path to UploadedFile instance(Laravel - UploadedFile 实例的文件路径)

本文介绍了Laravel - UploadedFile 实例的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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.

这篇关于Laravel - UploadedFile 实例的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel - UploadedFile 实例的文件路径

基础教程推荐