Upload File trong Laravel rất đơn giản. Bạn chỉ cần tạo một View chứa form upload file và một controller để xử lý file upload
# Store Upload file
Để lưu trữ các file đã được upload lên trên request các bạn sử dụng phương thức store
với cú pháp sau:
$request->file('photo')->store($path, $diskType);
Trong đó:
$path
là path mà bạn muốn lưu trữ file.$diskType
là loại disk mà bạn muốn lưu trữ. Các thông tin này các bạn có thể config ở trongconfig/filesystems.php
. Mặc định thì laravel sẽ sử dụng disk làlocal
, loại disk này sẽ lưu trữ thông tin vào trong thư mụcstorage/app/
Ví dụ: Lưu trữ ảnh vào trong thư mục storage/app/images
$path = $request->file('photo')->store('images');
//or
$path = $request->file('photo')->store('images', 'local');
Khi các bạn sử dụng phương thức store
để lưu trữ file, tên của file khi được lưu trữ sẽ là một chuỗi ngẫu nhiên 40 ký tự được sinh ra bởi Str::random(40)
trong Collection
Nếu bạn muốn đặt tên file theo cách của bạn thì có thể sử dụng phương thức storeAs
với cú pháp sau:
$request->file('photo')->storeAs($path, $fileName, $diskType);
Trong đó: $fileName
là tên của file mà các bạn muốn lưu trữ
Ví dụ:
$request->file('image')->storeAs('images', 'avatar.jpg', 'local');
Ngoài ra bạn cũng có thể sử dụng phương thức move
để lưu trữ file. Nhưng đối với phương thức này thì root folder sẽ được tính từ public/
Ví dụ: Lưu trữ image vào thư mục public/images
$request->file('image')->move('images', 'avatar.jpg', 'local');
# Ví dụ upload file trong Laravel
Ví dụ 1: Upload file đơn giản
Đầu tiên mình sẽ tạo ra route
để hiển thị ra form upload file và xử lý việc upload file
routes/web.php
use Illuminate\Http\Request;
Route::get('uploadfile', function () {
return view('formUploadFile');
});
Route::post('upload', function(Request $request) {
//Kiểm tra xem file đã được upload chưa
if(!$request->hasFile('image')) {
//Nếu chưa có file upload thì báo lỗi
return 'Hãy chọn file để upload';
}
else {
//Xử lý file upload
$image = $request->file('image');
//Lưu trữ file tại public/images
$imagePath = $image->move('images', $image->getClientOriginalName());
return 'Lưu trữ file thành công';
}
})->name('upload.handle');
Trong đó:
Route::post('upload', function(Request $request)
… Trả về url .../upload
sau khi Upload file thành công
$image->getClientOriginalName())
lấy tên file lưu trữ theo tên file được upload lên
resources/views/formUploadFile.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>File Manager</title>
</head>
<body class="antialiased">
<form action="{{ route('upload.handle') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="image">
<input type="submit" value="Submit">
</form>
</body>
</html>
Kết quả
Sau khi upload file thành công
Ví dụ 2: Upload file, hiển thị thông tin file qua Controller
resources/views/uploadfile.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>File Manager</title>
</head>
<body class="antialiased">
<form action="{{ route('uploadfile') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="image">
<input type="submit" value="Submit">
</form>
</body>
</html>
app/Http/Controller/UploadFileController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UploadFileController extends Controller
{
public function index() {
return view('uploadfile');
}
public function showUploadFile(Request $request) {
$file = $request->file('image');
//Display File Name
echo 'File Name: ' . $file->getClientOriginalName() . '<br>';
//Display File Extension
echo 'File Extension: ' . $file->getClientOriginalExtension() . '<br>';
//Display File Real Path
echo 'File Real Path: ' . $file->getRealPath() . '<br>';
//Display File Size
echo 'File Size: '. $file->getSize() . '<br>';
//Display File Mime Type
echo 'File Mime Type: '.$file->getMimeType();
//Move Uploaded File
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());
}
}
?>
routes/web.php
use App\Http\Controllers\UploadFileController;
Route::get('uploadfile', [UploadFileController::class, 'index'])->name('uploadfile');
Route::post('uploadfile', [UploadFileController::class, 'showUploadFile']);
Kết quả sau khi upload file thành công
Leave a Comment