php Laravel 5.3 Storage::put 创建一个包含文件名的目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40002275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:05:19  来源:igfitidea点击:

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

phplaravellaravel-5laravel-5.3laravel-filesystem

提问by zundi

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

我正在使用 Laravel 的文件存储功能来保存文件:

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

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

or

或者

storage/app/234234234.jpg/234234234.jpg

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!

谢谢!

回答by ABDEL-RHMAN

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

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

回答by xmhafiz

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

发生这种情况是因为您指定了要存储为文件名的目录。的newFilename,应该是目录名称,如“图像”。参考这一行

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

然后你会得到存储在的路径 storage/app/images/234234234.jpg

回答by AK Coders

Storage::disk('local')->putFileAs('', $file, $filenewname);

回答by Abid Shah

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

$file 已编码。您必须取消对文件的编码。

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

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