Laravel - file path to UploadedFile instance(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 实例的文件路径
基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
