I am using file_put_contents($file, $data);
function for putting contents in file, because these files are created on fly with name of sessions, $data is an multi-dimensional array, when i am echoing array it prints out fine but in file no contents get recorded except the word Array
What should i do or is there any other function which automatically creates the file and records the data (array)?
Thank You.
If you want to save a variable in a text file, you could use the serialize() / unserialize() functions for that: $data = array(1, 2, 3, ''); $toBeSaved = serialize($data); file_put_contents('somefile. txt', $toBeSaved);
The file_put_contents() writes data to a file. This function follows these rules when accessing a file: If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of filename. Create the file if it does not exist. Open the file.
The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.
You want to serialize()
the array on writting, and unserialize()
after reading the file.
$array = array('foo' => 'bar');
file_put_contents('foo.txt', serialize($array));
$array = unserialize(file_get_contents('foo.txt')));
Oh, and I really don't now how you echo'd your array, but echo array('foo' => 'bar');
will always print Array
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With