📜  在 laravel 中创建一个文本文件 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:54:25.740000             🧑  作者: Mango

代码示例1
namespace App\Http\Controllers;
use File;
class FileController extends Controller
{
    public function downloadJSONFile(){
      $data = json_encode(['Element 1','Element 2','Element 3','Element 4','Element 5']);
      $file = time() .rand(). '_file.json';
      $destinationPath=public_path()."/upload/";
      if (!is_dir($destinationPath)) {  mkdir($destinationPath,0777,true);  }
      File::put($destinationPath.$file,$data);
      return response()->download($destinationPath.$file);
    }
}