Laravel 5.3 File Uploading - To upload a file (image) in laravel 5.3 is so easy, we just need to create a controller, FileUpload Controller. at the previews Lessons, we have learn about Validation please read How to use Validation in Laravel 5.3 and about Sessions How to use Method HTTP Session in laravel 5.3.
Create File Upload in laravel 5.3
First, we need to create new Controller, please create FileuploadingController using Artisan CLI following by this command
php artisan make:controller FileuploadingController
Your controller will stored on app\Http\Controllers\FileuploadingController.php, Now Create function in the FileuploadingController.php
FileuploadingController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class FileuploadingController extends Controller
{
// create function for our upload page
public function index(){
return view('uploadfile');
}
// create new function for show uploaded page
public function showfileupload(Request $request){
$file = $request -> file('image');
// show the file name
echo 'File Name : '.$file->getClientOriginalName();
echo '<br>';
// show file extensions
echo 'File Extensions : '.$file->getClientOriginalExtension();
echo '<br>';
// show file path
echo 'File Path : '.$file->getRealPath();
echo '<br>';
// show file size
echo 'File Size : '.$file->getSize();
echo '<br>';
// show file mime type
echo 'File Mime Type : '.$file->getMimeType();
echo '<br>';
// move uploaded File
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());
}
}
Next, we need to create new Views and rename it with uploadfile.blade.php that save on resources\views\uploadfile.blade.php
uploadfile.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Upload File</title>
</head>
<body>
<?php
echo Form::open(['url'=>'/uploadfile','files'=>'true']);
echo 'Please Select a File to Upload ';
echo '<br>';
echo Form::file('image');
echo '<br>';
echo Form::submit('Upload File');
echo '<br>';
echo Form::close();
?>
</body>
</html>
Next, add new routes, that Routes file stored on routes\web.php
web.php
Route::get('/uploadfile', 'FileuploadingController@index');
Route::post('/uploadfile', 'FileuploadingController@showfileupload');
After all finished, try to access our upload file project using your browser by following this command
http://localhost:8080/uploadfile
Video tutorial Create File Upload in laravel 5.3
See you next Lessons
Không có nhận xét nào:
Đăng nhận xét