• Developer New

    Friday, March 24, 2017

    Create File Upload in laravel 5.3

    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
    

    No comments:

    Post a Comment

    Fashion

    Beauty

    Travel