36

I'm using Laravel's file storage functionality to save a file:

public function dataPost(Request $request) {

    $fileInForm = 'doc';

    if ($request->hasFile($fileInForm)) {

        $file = $request->file($fileInForm);
        if ($file->isValid()) {

            // Filename is hashed filename + part of timestamp
            $hashedName = hash_file('md5', $file->path());
            $timestamp = microtime() * 1000000;

            $newFilename = $hashedName . $timestamp . '.' . $file->getClientOriginalExtension();

            Storage::disk('local')->put($newFilename, $file);
        }
    }
}

This does save the file, but inside a directory named the same as the file, for example:

storage/app/952d6c009.jpg/952d6c009.jpg

or

storage/app/234234234.jpg/234234234.jpg

Is this expected? Is there any way to just store the file without a separate directory for each file?

Thanks!

zundi
  • 2,361
  • 1
  • 28
  • 45

6 Answers6

58

you need to provide the file contents in the second argument not file object, try this:

Storage::disk('local')->put($newFilename, file_get_contents($file));

ABDEL-RHMAN
  • 2,904
  • 16
  • 22
  • Thank you, been searching on this issue for hours, it doesn't seem to be specified anywhere, and the Laravel docs only give a txt as example with a string as input. – MrEvers Aug 05 '21 at 09:46
12

This happened because you specify the directory to store as filename. The newFilename, should be the directory name such as 'images'. Refer to this line

Storage::disk('local')->put($newFilename, $file);

So you could change this to

Storage::disk('local')->putFile('images', $file);

Then you will get the path stored at storage/app/images/234234234.jpg

xmhafiz
  • 3,482
  • 1
  • 18
  • 26
  • In this case at what point does the file get the new filename? Should I edit the `$file` object to update the filename? Something like `$file->originalName = $newFilename;` ? – zundi Oct 12 '16 at 16:38
  • you could do that or use @ABDEL-RHMAN – xmhafiz Oct 13 '16 at 00:40
12
Storage::disk('local')->putFileAs('', $file, $filenewname);
Blue
  • 22,608
  • 7
  • 62
  • 92
AK Coders
  • 129
  • 1
  • 2
  • 6
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/206345) by showing _why_ this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Blue Feb 14 '19 at 15:42
1

$file is encoded.You have to unencode the file.

Storage::disk('local')->put($newFilename, File::get($file));
Yousef Altaf
  • 2,631
  • 4
  • 46
  • 71
Abid Shah
  • 325
  • 3
  • 5
1

I know it's very old question, today I had the same issue and I tried

Storage::disk('local')->put($newFilename, file_get_contents($file));

it was very useful but in my case I wanted to the file inside folders like folder/folder/my_file.pdf

this solution didn't help.

I also tried this Storage::put("folder/folder/my_file.pdf", $contents);

didn't help too.

Found this magical function called storeAs and it made the trick Here is one Example from my code.

$this->residence_paper->storeAs('folder/another_folder', $residence_paper.'.pdf');

Worked fine for me.

Yousef Altaf
  • 2,631
  • 4
  • 46
  • 71
0

To create new folder or upload image to already created folder using laravel 8.83.27 Storage function

use Illuminate\Support\Facades\Storage;

$path     = 'uploads/employee/";
$filename = "testemp.pdf";                // For any file format
$image    = $request->image;              //base 64 format
Storage::disk('public')->put($path."/".$filename, base64_decode($image));
Sreejith N
  • 25
  • 5