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

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

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 lesson 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 page.so, basically how to give create file and download that file lesson below.

Route :

Route::get('downloadJSONFile', array('as'=> 'downloadJSONFile', 'uses' => 'JSONFileController@downloadJSONFile'));

JSONFileController Controller :

namespace App\Http\Controllers;

use View;

use File;

use Response;

class JSONFileController extends Controller

{

public function downloadJSONFile(){

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

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

File::put(public_path('/upload/json/'.$fileName),$data);

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 lesson.....

Tags

Recent Posts

Related Posts