Add file upload function. modify paramiter FILESYSTEM_DISK=public in file .env

This commit is contained in:
paoloGuagnano
2024-02-11 18:25:29 +01:00
parent f69f2ba046
commit 0cedec9711
10 changed files with 240 additions and 3 deletions

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StorefileRequest;
use App\Http\Requests\UpdatefileRequest;
use App\Models\file;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class FileController extends Controller
{
public function datasubmit(Request $request): RedirectResponse
{
//upload file in public folder
//dd($reqest->all());
//$filename = 'new_file.'.$request->filename->extension();
//$request->filename->move(public_path('uploads'), $filename);
//dd($request->all());
$filename = 'new_file.'.$request->filename->extension();
$request->filename->storeAs('file_temp', $filename);
return redirect('/words');
}
public function delete(): RedirectResponse
{
Storage::delete('file_temp/new_file.txt');
return redirect('/words');
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StorefileRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
];
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdatefileRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
];
}
}

11
app/Models/file.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class file extends Model
{
use HasFactory;
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Policies;
use App\Models\User;
use App\Models\file;
use Illuminate\Auth\Access\Response;
class FilePolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
//
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, file $file): bool
{
//
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
//
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, file $file): bool
{
//
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, file $file): bool
{
//
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, file $file): bool
{
//
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, file $file): bool
{
//
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\file>
*/
class FileFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
//
];
}
}

View File

@@ -0,0 +1,27 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('files');
}
};

View File

@@ -0,0 +1,17 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class FileSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//
}
}

View File

@@ -9,9 +9,10 @@
</p>
</header>
<form class="mt-6 space-y-6">
<x-text-input type="file" id="myFile" name="filename"/>
{{--<input type="submit">--}}
<form action="{{ route('store') }}" method="post" class="mt-6 space-y-6" enctype="multipart/form-data">
@csrf
<x-text-input type="file" id="filename" name="filename"/>
<div class="flex items-center gap-4">
<br><br><br><br>
<x-primary-button>{{ __('Submit') }}</x-primary-button>

View File

@@ -2,6 +2,7 @@
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\WordsController;
use App\Http\Controllers\FileController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
@@ -60,6 +61,9 @@ Route::get('delete_record/{id}', [WordsController::class, 'delete']);
Route::get('edit_record/{id}', [WordsController::class, 'edit']);
Route::post('update_record/{id}', [WordsController::class, 'update'])->name('words.update');;
//***** FILE *****\\
Route::post('datasubmit', [FileController::class, 'datasubmit'])->name("store");
require __DIR__ . '/auth.php';