Tutorial - Simple laravel Uploading File. Mengupload File di Laravel sangat mudah. Semua yang perlu kita lakukan adalah untuk membuat tampilan file di mana pengguna dapat memilih file yang akan di-upload dan controller di mana file upload akan diproses.
[Image]
Dalam view. Kita hanya perlu untuk membuat file input dengan menambahkan baris kode berikut:
Form::file('file_name');
In Form::open(), kita perlu menambahkan ‘files’=>’true’.
Form::open(array('url' => '/uploadfile','files'=>'true'));
Example
Langkah 1 − Buat sebuah view misal : resources/views/uploadfile.php, copy kode dibawah ini:
resources/views/uploadfile.php
<html>
<body>
<?php
echo Form::open(array('url' => '/uploadfile','files'=>'true'));
echo 'Select the file to upload.';
echo Form::file('image');
echo Form::submit('Upload File');
echo Form::close();
?>
</body>
</html>
Langkah 2 − Buat controller UploadFileController dengan artistan command.
php artisan make:controller UploadFileController --plain
Langkah 3 - Copy kode dibawah ini app/Http/Controllers/UploadFileController.php file.
app/Http/Controllers/UploadFileController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
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();
echo '<br>';
//Display File Mime Type
echo 'File Mime Type: '.$file->getMimeType();
//Move Uploaded File
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());
}
}
Langkah 4 - Tambahkan route di app/Http/routes.php.
app/Http/routes.php
Route::get('/uploadfile','UploadFileController@index');
Route::post('/uploadfile','UploadFileController@showUploadFile');
Langkah 5 - Buka url:
http://localhost:8000/uploadfile
Mudah bukan? terima kasih sudah mengikuti tutorial ini, Mudah-mudahan membantu bagi yang baru belajar laravel.
posted by Brillian Musyafa at 11:55 AM on Sep 7, 2016
"Tutorial - Simple Laravel Uploading File"
No comments yet. -