file_exists()和过期的缓存不遵循逻辑

时间:2011-08-15 01:49:26

标签: php

逻辑上我试图找出为什么我的代码无效。

foreach($list as $persona){    
//If file exists

            if(file_exists('templates/cache/' . $persona['toolbar_id'] . 'txt')){
                //file is expired older than 10 seconds for testing purposes
                if(time() - filemtime('/templates/cache/' . $persona['toolbar_id'] . '.txt')  >= 10)  {


                // jSON URL which should be requested
                $json_url = 'example';

                // Initializing curl
                $ch = curl_init( $json_url );

                // Configuring curl options
                $options = array(
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
                );

                // Setting curl options
                curl_setopt_array( $ch, $options );

                // Getting results
                $result =  curl_exec($ch); // Getting jSON result string    
                file_put_contents('templates/cache/' . $persona['toolbar_id']. '.txt', $result);            
                $result = json_decode($result, true);               
                $result = $result[0];
                echo 'expired-recreating cache';
                }                   

            $result = json_decode(file_get_contents('templates/cache/' . $persona['toolbar_id']. '.txt'), true);
            $result = $result[0];                   


            }

            // jSON URL which should be requested
            $json_url = ''';

            // Initializing curl
            $ch = curl_init( $json_url );

            // Configuring curl options
            $options = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
            CURLOPT_POSTFIELDS => $json_string
            );

            // Setting curl options
            curl_setopt_array( $ch, $options );

            // Getting results
            $result =  curl_exec($ch); // Getting jSON result string    
            file_put_contents('templates/cache/' . $persona['toolbar_id']. '.txt', $result);            
            $result = json_decode($result, true);               
            $result = $result[0];
            echo 'didnt exist';
}

我总是以“不存在”的回声结果结束。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

我的第一个观察可能是解决方案,你有:

  if(file_exists('templates/cache/' . $persona['toolbar_id'] . 'txt')){

你不能错过txt之前的一个点吗?我相信它应该是:

 if(file_exists('templates/cache/' . $persona['toolbar_id'] . '.txt')){

在其他所有方法中,例如:

 file_put_contents('templates/cache/' . $persona['toolbar_id']. '.txt', $result);

你有.txt:)