Lluís Puig Ferrer Lluís Puig Ferrer - 3 years ago 81
PHP Question

Laravel - Need specific name of files and folders with storage function

i have a form with slug, input files, etc.

I need if i put por example in form:

slug = hello-how-are-you


file = header.png


file2 = home.png


Laravel
create me a folder in storage like this:
/projects/hello-how-are-you
with the
header.png
and
home.png
.

I try it but can't do it.. i try with something like this:

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


In filesystem i have this:

'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'),
],

],

Answer Source

This might do the trick for you:

public function storeProject(Request $request)
{
    $project = Project::create($request->only('slug', 'position', 'public', 'pathheader', 'pathhome'));

    \Storage::disk('projects')->makeDirectory($project->slug);

    foreach ($request->allFiles() as $file) {
        \Storage::disk('projects')->putFileAs($project->slug, $file, $file->getClientOriginalName());
    }
}

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download