Rahul
Rahul

Reputation: 1079

Unable to save my files with Storage::put($file, $content) in laravel 5.2

Just a simple doubts-

i'm trying to store my image and video file on storage using Storage facade. but i'm confused because i'm unable to store that file on storage folder. i'm using intervention image package and when i just try to save my modified $file

Storage::put($file); it's throwing error that 2 argument missing etc.

i searched everywhere on the net regarding this issue and i found

Storage::put($file , $content) now what is $content in this case when i'm trying just save an image to my folder ? please give me example-

and one more thing

How do i return a full path of that file which can be accessible from different domain. i'm building an API :)

Thanks for the help in advance

Upvotes: 5

Views: 25946

Answers (2)

Chris Forrence
Chris Forrence

Reputation: 10114

The second parameter in Storage::put($filename, $content) refers to the raw information that makes up the file. For a text file, $content would be the text, while for an image, it'd be the raw information that makes up the image.

Since you're using Intervention Image, you can use the encode() method to get the data needed for storage.

// Assuming that $file is your Intervention Image instance
// Example 1: Save as the default type (JPEG)
Storage::put('public/images/my_profile.jpeg', $file->encode());

// Example 2: Save as a PNG
Storage::put('public/images/my_profile.png', $file->encode('png'));

In regards to your question about how to refer to that path as a URL, make sure to follow the following snippet from the documentation:

Note: When using the local driver, be sure to create a symbolic link at public/storage which points to the storage/app/public directory.

After that, you'll be able to access everything from storage/app/public from the browser like such:

echo config('app.url') . str_replace('/public/', '/', Storage::url('public/images/my_profile.png'));
// Echoes "http://localhost/storage/images/my_profile.png"

Upvotes: 6

Simon Davies
Simon Davies

Reputation: 3684

What disk your using 'public', 'local' basically where your storing it?

   \Storage::disk('local')->put($file_name, $the_file);

The disk settings can be view in the config/filesystems.php file.

Also the $content is the actually item/file you want to save. Here is an example of what ive done recently..

   // the $image is from $request->file('file');
   $thefile = \File::get($image);
   $file_name = $title->slug . '-' . $year . '.jpg';
    \Storage::disk('local')->put($file_name, $thefile);

Upvotes: 5

Related Questions