create a text file in laravel

Solutions on MaxInterview for create a text file in laravel by the best coders in the world

showing results for - "create a text file in laravel"
Tommaso
29 Nov 2020
1namespace App\Http\Controllers;
2use File;
3class FileController extends Controller
4{
5    public function downloadJSONFile(){
6      $data = json_encode(['Element 1','Element 2','Element 3','Element 4','Element 5']);
7      $file = time() .rand(). '_file.json';
8      $destinationPath=public_path()."/upload/";
9      if (!is_dir($destinationPath)) {  mkdir($destinationPath,0777,true);  }
10      File::put($destinationPath.$file,$data);
11      return response()->download($destinationPath.$file);
12    }
13}