Laravel 5.1 使用存储将文件移动到公共文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38628116/
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-09-14 14:13:21  来源:igfitidea点击:

Laravel 5.1 using storage to move the file to a public folder

phplaravellaravel-5.1

提问by techwestcoastsfosea

In my Laravel API call, I use Storage to upload a file. Then I wold like to rename and move the file to a public directory by using:

在我的 Laravel API 调用中,我使用 Storage 上传文件。然后我想使用以下命令重命名文件并将其移动到公共目录:

    $dest = '/var/www/public/uploads/';
    Storage::disk('local')->put($filename.'.'.$extension,  File::get($file));
    $oldfilename = $filename.'.'.$extension;
    Storage::move($oldfilename, $dest.$newImageName);

But instead of moving the file to the $dest directory, I get a folder created with the $dest dir name inside my storage/app folder and the file is copied there. I have also tried using double quotes around $dest.$newImageNamebut no success.

但是我没有将文件移动到 $dest 目录,而是在我的 storage/app 文件夹中使用 $dest 目录名称创建了一个文件夹,并将文件复制到那里。我也尝试过使用双引号,$dest.$newImageName但没有成功。

回答by thefallen

If you have an array ['disks']['public']in config/filesystems.phpyou can do:

如果您['disks']['public']config/filesystems.php 中有一个数组,您可以执行以下操作:

Storage::disk('public')->move($oldfilename, $dest.$newImageName);

If you don't create one that looks like this:

如果你不创建一个看起来像这样的:

    'public' => [
        'driver'     => 'local',
        'root'       => storage_path( 'app/public' ),
        'visibility' => 'public',
    ],

And then you'll bi able to use the public"disk" with the storage facade.

然后你就可以使用带有存储外观的公共“磁盘”。

Note that you don't have to specify the absolute path anymore, Laravel will know that the storage path is app/public.

请注意,您不必再指定绝对路径,Laravel 会知道存储路径是app/public

EDIT

编辑

Another config option that might work:

另一个可能有效的配置选项:

    'public' => [
        'driver'     => 'local',
        'root'       => public_path(),
        'visibility' => 'public',
    ],