0

I am trying to create a json file and each time I enter the page using this.

if(!isset($_SESSION['backup_active']))
 {
    $backup=fopen("backup/".time().".json", "w");
    fwrite($backup, json_encode($list)); fclose($backup);
     $_SESSION['backup_active']=true;
 }

What this does is that when I enter the page it creates the backup but when I delete the backup file and try again it wont create a new one. After I create my backup file I want to call it and put it in another .json file like this.

session_start();
                if(isset($_SESSION['backup_active']))
                {
                    $fBackup= "backup";
                    $lastFile= end($fBackup);
                    $listFile = file_get_contents($lastFile);
                    file_put_contents('list.json', $listFile); 
                    session_destroy();
                }  

But this doesn't seem to work. I use the end() method to take the last made backup file and then put it in the list.json file.

Can anyone help me fix this problem?

Akshay Hazari
  • 3,186
  • 4
  • 48
  • 84
Ronald Dewindt
  • 65
  • 1
  • 11
  • 1
    _“when I delete the backup file and try again it wont create a new one”_ – what do you mean, you delete the file manually and then call the first script again? Then that’s probably because your session still contains the entry `backup_active` with value `true`, so your script doesn’t do anything. – CBroe Dec 04 '15 at 11:56
  • manually!! because when i refresh page it wont create another backup, so i tried deleting it manually to see if its creates another one but it wont, the folder stays empty – Ronald Dewindt Dec 04 '15 at 11:59
  • 1
    one more problem in your code that you can't so easy with end() function take last file from directory you have to use one of this scripts mentioned here in answers - http://stackoverflow.com/questions/1491020/php-get-the-latest-file-addition-in-a-directory – Armen Dec 04 '15 at 11:59
  • 1
    Well if you only delete the file, but don’t update the value of `backup_active` in your session, of course it won’t write a new file then, because that’s how you explicitly implemented it. – CBroe Dec 04 '15 at 12:05
  • What would be a better way to do this? – Ronald Dewindt Dec 04 '15 at 12:41
  • Clear your session. Or implement logic to delete a file _within_ your script, so that it takes care of resetting the session value then. – CBroe Dec 04 '15 at 13:06

1 Answers1

-1

Why is this code not working, after I try to get the $latest_filename I get a bool(false) as output on var_dump. Why do I get this. When I try something like this:

 $backupFile = file_get_contents("listbackup.json");
 file_put_contents('list.json', $backupFile);

It works but when I try this code here it doesnt why? And how can I fix this.

session_start();

if (isset($_SESSION['active'])) {
    $path = "backup";
    $latest_ctime = 0;
    $latest_filename = '';
    $d = dir($path);
    while (false !== ($entry = $d->read())) {
        $filepath = "{$path}/{$entry}";
        if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
            $latest_ctime = filectime($filepath);
            $latest_filename = $entry;
        }
    } * *$latest = file_get_contents($latest_filename);
    file_put_contents('list.json', $latest); * *session_destroy();
}
Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
Ronald Dewindt
  • 65
  • 1
  • 11