Laravel - path to the UploadedFile instance

I have a Laravel 4.2 API that accepts files when creating a resource. The file is extracted using Input::file('file')

Now I want to write a script (also in Laravel) that will create multiple resources (therefore, I cannot use an HTML form that sends POST to the API endpoint). How can I translate the file path into an instance of UploadedFile so that Input::file('file') takes it into the API?

+5
source share
2 answers

Just create the instance yourself. API:

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

So you should be able to:

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

Note: You must specify the 6th build parameter as TRUE, so the UploadedFile class knows that you are loading the image through a testing environment.

+8
source
  /** * Create an UploadedFile object from absolute path * * @static * @param string $path * @param bool $public default false * @return object(Symfony\Component\HttpFoundation\File\UploadedFile) * @author Alexandre Thebaldi */ public static function pathToUploadedFile( $path, $public = false ) { $name = File::name( $path ); $extension = File::extension( $path ); $originalName = $name . '.' . $extension; $mimeType = File::mimeType( $path ); $size = File::size( $path ); $error = null; $test = $public; $object = new UploadedFile( $path, $originalName, $mimeType, $size, $error, $test ); return $object; } 
+1
source

Source: https://habr.com/ru/post/1202510/


All Articles