Laravel 5.3 Storage::put creates a directory with the file name

zundi picture zundi · Oct 12, 2016 · Viewed 56.3k times · Source

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!

Answer

ABDEL-RHMAN picture ABDEL-RHMAN · Oct 12, 2016

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));