I’m trying to write an array or object to a file in plain-text (Not Binary), however whenever I loop over the array and look at the file I’m receiving Binary instead.
$this->user and $this->temp_files['in_file_name'] are passed into the function prior.
$user_array = (array)$this->user;
$data = "";
foreach ($user_array as $key => $value) {
$data .= "$key $value\n";
}
file_put_contents($this->temp_files['in_file_name'], $data);
If i just pass a string into $data it gets written to the file as plain-text, but when I loop over the object or array I receive binary. Is there a way to get the plain-text version of this array in the file?
$this->user is a database row as an object from Ion_Auth called by:
$this->user =$this->ion_auth->user($this->user_id)->row();
Here is a portion of the output of the file
4461 7465 2054 7565 7364 6179 204f 6374 6f62 6572 2033 302c 2032 3031 3269 6420 320a 6970 5f61 6464 7265 7373 207f 0000 010a 7573 6572 6e61 6d65 2062 7261 6e64 6f6e 2062 6f73 7765 6c6c 0a70 6173 7377 6f72 6420 6539 3936 3630 6665 3039 3535 6663 3862 3431 6639 3838 3066 3639 6131 6664 6635
The var_dump of the user array is: (I’ve changed some values for data security reasons)
array (size=22)
'id' => string '2' (length=1)
'ip_address' => string '��' (length=4)
'username' => string 'brandon' (length=15)
'password' => string 'changed' (length=40)
'salt' => null
'email' => string 'brandon@changed.net' (length=27)
'activation_code' => null
'forgotten_password_code' => null
'forgotten_password_time' => null
'remember_code' => string 'changed' (length=40)
'created_on' => string '1337702147' (length=10)
'last_login' => string '1351607850' (length=10)
'active' => string '1' (length=1)
'first_name' => string 'Brandon' (length=7)
'last_name' => string 'changed' (length=7)
'company' => string 'changed' (length=17)
'phone' => string '804-814-changed' (length=12)
'store' => string 'mechanicsville' (length=14)
'delivery' => string '1' (length=1)
'receiveEmails' => string '1' (length=1)
'customerType' => string 'business' (length=8)
'user_id' => string '2' (length=1)
If I do a file_get_contents on the file it can be echoed back in correctly, but I wish the file were stored as plain-text.
Decoding the hex bytes that you’ve posted reveals the correct data. So I’ll take a guess: PHP is writing the file perfectly, but the raw null bytes in the IP address (127.0.0.1) are confusing the OS or editor you are using to open the file, into thinking that this is not a text file, so it’s displaying it in a hex editor view for you. Try opening the file in a different editor, or saving the
'ip_address'field as text.