Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store data into a text file using php?

Tags:

php

i am trying to store data in a text file, something like an array into a text file using php instead of storing into mysql database.

for example here are the data to be stored in a text file

name=>john
age=>25
location=>australia

then after saving it to a text file , how can I get the contents out and parse it with php , for like php can find the name , age and location and echo it out(something like parsing an array)

I need it for storing the data into a text file so it can be easily access from other domains without requiring to be on the same domain , connect to database , get the data from database. I am looking for a speedy solution. :)

I'm not sure which direction should I look into for this kind of functionality , hoping someone can point me out.

like image 260
sm21guy Avatar asked Dec 06 '22 12:12

sm21guy


1 Answers

Storing:
1. Use serialize() to serialize your array into a string
2. Write that string to text file using file_put_contents()

Reading:
1. Use file_get_contents to read text file
2. Use unserialize() to unserialize previously serialized array

serialize()/unserialize() can be replaced by json_encode()/json_decode()

like image 54
Māris Kiseļovs Avatar answered Dec 28 '22 05:12

Māris Kiseļovs