PHP file_get_contents()无法解决问题

时间:2015-12-04 14:30:00

标签: php json session

有什么区别,我该如何解决这个问题。

为什么这个工作

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

当我像这样使用它时它不会工作。

这个在HTML

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

这一个追求

 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(); 
                        }

当我尝试从$ latest_filename获取内容时,它给了我一个虚假的Bool,我无法继续使用它。我需要的是将内容放在list.json中。

1 个答案:

答案 0 :(得分:1)

尝试更改

$latest_filename = $entry;

要:

$latest_filename = $path.'/'.$entry;

你在$path内寻找你的档案,但是当你试图到达它时,你需要再次进入$path,否则你将永远找不到它。