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 实例的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)