Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - file path to UploadedFile instance

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

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?

like image 294
whonoes Avatar asked Sep 13 '14 21:09

whonoes


People also ask

Where is storage path Laravel?

Laravel's filesystem configuration file is located at config/filesystems.php . Within this file, you may configure all of your filesystem "disks". Each disk represents a particular storage driver and storage location.

How can I get storage path in Laravel 8?

Laravel 8.x < You can use the storage_path(); function to get storage folder path.

What is public path in Laravel?

This is a typical Laravel folder structure, with the default public folder : / /app /bootstrap /config /database /public /index.php /resources /routes /storage. This is specially useful when you try to deploy or publish your application in a standard web server.

How to store Uploaded file in Laravel?

To upload files to storage in Laravel, use MAMP or XAMPP as a local web server, define a database name in MySQL, and add the correct configuration in the . env file.


1 Answers

Just construct an instance yourself. The API is:

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

So you should be able to do:

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

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.

like image 186
Cody Caughlan Avatar answered Oct 26 '22 00:10

Cody Caughlan