Laravel - 需要具有存储功能的文件和文件夹的特定名称

时间:2017-07-25 08:00:37

标签: php laravel

我有一个带有slug,输入文件等的表单。

如果我把例子放在表格中,我需要:

slug = hello-how-are-you

file = header.png

file2 = home.png

Laravel在存储中创建一个文件夹,如下所示:/projects/hello-how-are-youheader.pnghome.png

我尝试但不能这样做..我尝试这样的事情:

public function storeProject(Request $request)
{
    $project = new Project();
    $project->slug = $request->input("slug");
    $namefolder = $project->slug;
    $project->position = $request->input("position");
    $project->public = $request->input("public");
    $header = $request->file('pathheader');
    $home = $request->file('pathhome');
    $project->pathheader = $header;
    $project->pathhome = $home;
    \Storage::disk('projects')->put('header.png', \File::get($header));
    $project->save();
}

在文件系统中我有这个:

'disks' => [

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

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],
    'projects' => [
        'driver' => 'local',
        'root' => storage_path() . '/projects',
    ],
    's3' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

],

解决! 感谢@ Dees040 如果想要粉碎文件的名称并在没有foreach的情况下进行,你可以这样做:

public function storeProject(Request $request)
        {
        $project = new Project();
        $project->slug = $request->input("slug");
        $project->position = $request->input("position");
        $project->public = $request->input("public");
        $project->pathheader = $request->file('pathheader');
        $project->pathhome = $request->file('pathhome');
        \Storage::disk('projects')->makeDirectory($project->slug);
        \Storage::disk('projects')->putFileAs($project->slug,$project->pathheader,'header.png');
        \Storage::disk('projects')->putFileAs($project->slug,$project->pathhome,'home.png');
        $project->save();
        }

2 个答案:

答案 0 :(得分:1)

这可能适合你:

Int32

答案 1 :(得分:0)

首先创建目录:

use Illuminate\Support\Facades\Storage;

Storage::disk('projects')->makeDirectory($namefolder);

然后,存储文件:

$project->pathheader = Storage::disk('projects')->putFileAs($namefolder, $header, $header->getClientOriginalName());
$project->pathhome = Storage::disk('projects')->putFileAs($namefolder, $home, $home->getClientOriginalName());