How to create a text file for JSON and file download in Laravel?

By Hardik Savani January 6, 2016 Category : Laravel

If you want create your own text file, html file, css file, php file, json file etc in Laravel. then we do not need to use any PHP library or Class because Laravel Framwork Provide its own "File" Facade. File Class through you can create your own html file, txt file, php file etc as you want. File Facade Class also provide several function like you can create file, remove file, update file etc. now i am giving you example of how to create json file in Laravel.

you also want to download file that you created now, i mean you want to give download file, then you can also give download that file using Laravel "Response" Class. in Laravel Response Class is use for give response or redirect something, one page to another, basically how to give create file and download that file example below.

Route :

Route::get('downloadJSONFile', array('as'=> 'downloadJSONFile', 'uses' => '[email protected]'));

JSONFileController Controller :

namespace App\Http\Controllers;

use View;

use File;

use Response;

class JSONFileController extends Controller


public function downloadJSONFile(){

$data = json_encode(['Example 1','Example 2','Example 3',]);

$fileName = time() . '_datafile.json';


return Response::download(public_path('/upload/jsonfile/'.$fileName));



you have to also use File and Response defile in top of your controller. don't forgot to use File Class and Response Class on your controller.

Try this example.....

Tags :

We are Recommending you