Sunday, 15 March 2015

storing and appending array in file in php -



storing and appending array in file in php -

i have app_id , current date. want update database if current date , stored date greater 4. i'm doing this:

$app_id = $_get['ap']; $current_date=date('d'); $a = array($app_id => $current_date); $fp = fopen("time.txt",'r'); $last_run = unserialize(file_get_contents("time.txt"))[$app_id]; if(abs($last_run-$current_date) > 2) { $fp = fopen("time.txt", 'w'); fwrite($fp, serialize($a)); }

but problem in when switch app 1 time again update database because i'm using writing mode on writing text file. can utilize append mode. how can search in array stored in file , particular date corresponding app_id , append date suitably. in advance

you need read file , unserialize whole array variable, can test , update part of array , write whole array file.

also file_get_contents() , file_put_contents() not need open file handle internally.

$app_id = $_get['ap']; $current_date = date('d'); $last_run = unserialize(file_get_contents("time.txt")); if(abs($last_run[$app_id] - $current_date ) > 2 ) { // alter lastly run date app $last_run[$app_id] = $current_date; file_put_contents('time.txt', serialize($last_run)); }

you ought doing checking in here well.

that 'ap' beingness passed script in $_get array that $last_run array contains $app_id occurance

php

No comments:

Post a Comment