From 6cf8bace5261c43888d420d00d723a08e299d0f9 Mon Sep 17 00:00:00 2001 From: Thuan Bui <9248622+10h30@users.noreply.github.com> Date: Tue, 6 May 2025 08:39:59 +0900 Subject: [PATCH] Complete Part 4 --- README.md | 11 +++---- app/Http/Controllers/UploadController.php | 24 +++++++++++++-- app/Models/Upload.php | 13 +++++++++ ...2025_05_05_115756_create_uploads_table.php | 29 +++++++++++++++++++ resources/views/upload.blade.php | 23 ++++++++++++++- routes/web.php | 1 + 6 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 app/Models/Upload.php create mode 100644 database/migrations/2025_05_05_115756_create_uploads_table.php diff --git a/README.md b/README.md index b7197f8..f511dfc 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,12 @@ 👉 Code cho từng phần nằm ở **các branch riêng**: -| Phần | Tiêu đề | Branch | -| ---- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------- | -| 1 | File Upload cơ bản trong Laravel | [`part-1-basic-upload`](https://github.com/10h30/laravel-file-upload-series/tree/part-1-basic-upload) | -| 2 | Validation & Bảo mật khi upload | [`part-2-validation-security`](https://github.com/10h30/laravel-file-upload-series/tree/part-2-validation-security) | -| 3 | Upload cùng lúc nhiều file | [`part-3-multiple-file-upload`](https://github.com/10h30/laravel-file-upload-series/tree/part-3-multiple-file-upload) | +| Phần | Tiêu đề | Branch | +| ---- | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | +| 1 | File Upload cơ bản trong Laravel | [`part-1-basic-upload`](https://github.com/10h30/laravel-file-upload-series/tree/part-1-basic-upload) | +| 2 | Validation & Bảo mật khi upload | [`part-2-validation-security`](https://github.com/10h30/laravel-file-upload-series/tree/part-2-validation-security) | +| 3 | Upload cùng lúc nhiều file | [`part-3-multiple-file-upload`](https://github.com/10h30/laravel-file-upload-series/tree/part-3-multiple-file-upload) | +| 4 | Hiển thị và xoá các file đã upload | [`part-4-manage-uploads`](https://github.com/10h30/laravel-file-upload-series/tree/part-4-manage-uploads) | | | > 📖 Mỗi branch tương ứng với một phần trong series blog. Bạn có thể clone và chạy từng phần riêng biệt để dễ theo dõi. diff --git a/app/Http/Controllers/UploadController.php b/app/Http/Controllers/UploadController.php index 3cfa1bb..61587ce 100644 --- a/app/Http/Controllers/UploadController.php +++ b/app/Http/Controllers/UploadController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\Upload; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; @@ -14,7 +15,8 @@ class UploadController extends Controller */ public function index() { - return view('upload'); + $uploads = Upload::latest()->get(); + return view('upload', compact('uploads')); } /** @@ -65,8 +67,14 @@ class UploadController extends Controller // Lưu file bằng storeAs với tên file mới $storedFilePath = $file->storeAs($directory, $finalFilename, $disk); // Trả về đường dẫn tương đối: 'uploads/ten_file_cuoi_cung.jpg' $storedFilePaths[] = $storedFilePath; // Thêm đường dẫn file đã lưu vào array $storedFilePaths - } + // Tạo bản ghi mới trong table uploads của database + Upload::create([ + 'filename' => $storedFilePath, + 'original_filename' => $originalFilename, + ]); + + } // Chuyển hướng về trang trước đó return back()->with('success', 'You have successfully uploaded ' . $numberOfFiles . ' files') @@ -75,4 +83,16 @@ class UploadController extends Controller // Gửi kèm array các tên file gốc vào session flash data với key 'original_filenames' ->with('original_filenames', $originalFilenames); } + + public function destroy(Upload $upload) + { + // Xoá file vật lý khỏi disk 'public' dựa vào đường dẫn lưu trong $upload->filename + Storage::disk('public')->delete($upload->filename); + + // Xoá bản ghi tương ứng trong database + $upload->delete(); + + // Chuyển hướng người dùng về trang trước đó với thông báo thành công + return back()->with('success', 'You have successfully deleted ' . $upload->original_filename); + } } diff --git a/app/Models/Upload.php b/app/Models/Upload.php new file mode 100644 index 0000000..a59eea7 --- /dev/null +++ b/app/Models/Upload.php @@ -0,0 +1,13 @@ +id(); + $table->string('filename'); + $table->string('original_filename'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('uploads'); + } +}; diff --git a/resources/views/upload.blade.php b/resources/views/upload.blade.php index c17bb0d..5e19f3a 100644 --- a/resources/views/upload.blade.php +++ b/resources/views/upload.blade.php @@ -47,7 +47,7 @@ @if (session("stored_paths") && is_array(session("stored_paths")))
Uploaded Files:
- {{-- Lặp qua array các đường dẫn file đã lưu. $index là chỉ số, $path là đường dẫn --}} + {{-- Lặp qua array các đường dẫn file đã lưu. $index là chỉ số, $path là đường dẫn --}} @foreach (session("stored_paths") as $index => $path)Original Filename: @@ -63,6 +63,27 @@