Make a Text File Using Laravel and PHP
In Laravel, use put() of Storage facade.
use Illuminate\Support\Facades\Storage;
Storage::disk('local')->put('file.txt', 'Your content here');
// 'file.txt' // yo can use your file name here.
// 'Your content here' // you can specify your content here
This will be stored in storage/app/
so your controller will look like this,
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Employee;
class HomeController extends Controller
{
public function store(Request $request)
{
Storage::disk('local')->put('file.txt', 'Your content here');
$employee = new Employee();
$employee->nama = $request->get('name');
$employee->email = $request->get('email');
$employee->dob = $request->get('date');
$employee->phone = $request->get('phone');
$employee->gender = $request->get('gender');
$employee->addreess = $request->get('addreess');
$employee->save();
return redirect('employees')->with('success','Selamat, Data berhasil di tambahkan !');
}
}
Muhammad Sholehhudin
Updated on June 04, 2022Comments
-
Muhammad Sholehhudin almost 2 years
I want to ask a question. I'm new in laravel framework. I want to create a text file from the input form data user using the Laravel framework. while the same time, I want to save these input to my database.
I have success to save in my database. but I fail to create the .txt file from this.
I have tried to get data from database but error. and I just try to get data from input form is an error too.
I've tried...
<?php $nama = $_POST['name']; $email = $_POST['email']; $dob = $_POST['date']; $phone = $_POST['phone']; $gender = $_POST['gender']; $address = $_POST['addreess']; $date = date('dmY'); $jam = data('his'); $data = "$nama,$email,$dob,$phone,$gender,$address"; $file = "$nama"-"$date$jam"; $namafile = "$file.txt"; $fh = fopen($namafile,"w"); fwrite($fh,$data); fclose($file); echo "<h2>Hasil Penyimpanan Data</h2>"; echo "<hr>"; echo "Hasil : <a href='$namafile'> $namafile </a>";
I use it in my index.php with the means that I can get data from the input form directly and then save as a .txt file.
Here's my code in the controller that's saving to my database:
public function store(Request $request) { $employee = new Employee(); $employee->nama = $request->get('name'); $employee->email = $request->get('email'); $employee->dob = $request->get('date'); $employee->phone = $request->get('phone'); $employee->gender = $request->get('gender'); $employee->addreess = $request->get('addreess'); $employee->save(); return redirect('employees')->with('success','Selamat, Data berhasil di tambahkan !'); }
How can I save the input data to the database and at the same time how can I save the data to a text file?
Thanks for your help. :)
-
ceejayoz over 5 yearsDo one thing, then do the second thing, then return the redirect. See laravel.com/docs/5.7/filesystem#storing-files for the Laravel way of working with files. If you want more help than that, let us know what "but error" means, like the error message and the lines of code that cause it.
-
aynber over 5 yearsYour filename won't be correct, since that's not how you concatenate. It might also be better to specify which directory you want to place it in, whether it's storage or public.
-
Muhammad Sholehhudin over 5 yearsactually i don't know where i have to put the syntax that i can save to txt file. i use that code in php native and in Codeogniter is work. when i use laravel, i confused how i can save the data to text file @ceejayoz
-
ceejayoz over 5 years@MuhammadSholehhudin Do it after you
->save()
the Employee record. Why are you even creating a file, though? You've got everything in the database already. -
Muhammad Sholehhudin over 5 yearscan i save this form to the txt file ? so this is the requirement from my bos.. i have to save the information form to the text file.. @ceejayoz
-
ceejayoz over 5 yearsYes, you can. I've linked you to the docs on how to create a file and save text into it. (It's still a silly requirement; what's the point of storing it both in a .txt and in the database?)
-
Muhammad Sholehhudin over 5 yearsi've tried to put it below ->save() but it still error. it says "fclose() expects parameter 1 to be resource, string given" . what i should to change it to be string..hmm yaa you know, the bos is always true in any situation.. @ceejayoz
-
ceejayoz over 5 yearsYou shouldn't be using functions like
fclose
. I linked you to the docs on Laravel's file functions.
-
-
Muhammad Sholehhudin over 5 yearswhere i should take this syntax ? in controller or route ? or any else ? i still newbie at all laravel.. @Sobin
-
Sobin Augustine over 5 years@MuhammadSholehhudin no worries, I can help you, you can add this code in your controller, but this will go at the top use Illuminate\Support\Facades\Storage;
-
Muhammad Sholehhudin over 5 yearsOke thankyou so much.. if you can guide me to clear this exercise. then where i add the Storage::disk('local')->put('file.txt', 'Your content here'); ? below my Storage() method ? or i have to create a new method and write in that ? @SobinAugustine
-
Sobin Augustine over 5 yearsNo, you can add both in the same method store(), if it's has to happen in the same process.
-
Muhammad Sholehhudin over 5 yearsSo, this code is still working or not ? about how i create these data to txt file ? i means the first code that i share above @SobinAugustine
-
Sobin Augustine over 5 yearsStep 1: do Storage::disk('local')->put('file.txt', 'Your content here');
-
Sobin Augustine over 5 yearsafter checking step 1 do Step 2: add your logic to file name and file content
-
Sobin Augustine over 5 yearsStep 3 : implement the eloquent save
-
Muhammad Sholehhudin over 5 yearsSo, my logic about file name and content is still in Controller ? with Storage method ? @SobinAugustine
-
Muhammad Sholehhudin over 5 yearsHey, you know, im just success to creating a file.. but its only one time.. then i fill the form again, the text file is not created..
-
Sobin Augustine over 5 years@MuhammadSholehhudin yes, with store() method
-
Sobin Augustine over 5 years@MuhammadSholehhudin thats because it's again overwriting the file, so give unique names(include timestamp in file name logic etc), that will solve the issue.
-
Muhammad Sholehhudin over 5 yearsYaa, you were right.. now this is work for me..i can add if the input name is different or after refreshing..hehe btw thank you so much before.. but there is one function doesnt working yet.. [$date = date('dmY'); $jam = data('his');] i can't do this code in my controller because controller doesn't extend bootstrap datepicker.. how can i get the value of the time now ? @SobinAugustine