We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7966
    • 90 Posts
    "file_get_contents" function is not working after clearing the cache or saving a resource page, when called the firs time.

    When called second time it works.

    I solved the problem by using the curl alternative, but I think there might be a bug related to the cache.

      • 18373 ☆ A M B ☆
      • 3,141 Posts
      file_get_contents is a php function, not something set by MODX...

      Where are you using this? What are you doing?
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 7966
        • 90 Posts
        Sorry late to response.

        I use that function to get the html source code of each page. Although it is a php function, I think it is behavior probably related with MODx caching. It might be a hidden bug.
          • 3749
          • 24,544 Posts
          Others have reported cache issues in 2.1. They may be fixed in the pl release (coming soon).

          Please report bugs here: http://bugs.modx.com/ so they won’t fall through the cracks.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 22303 MODX Staff
            • 10,725 Posts
            This is not bug if you are trying to include the cache file manually. This is not supported. Otherwise, you need to provide more information about what you are doing...
              • 7966
              • 90 Posts
              Quote from: OpenGeek at May 09, 2011, 11:04 AM

              This is not bug if you are trying to include the cache file manually. This is not supported. Otherwise, you need to provide more information about what you are doing...

              I created a Component. It calls the file_get_contents() function. I get the html content and save it to a physical file.

              I don’t touch to any caching mechanism.

              When file_get_contents is called after site--> Clear Cache call, it doesn’t get the page’s html content.

              It doesn’t also work after you save a document and try to get html content of that document through file_get_contents call.

              In both cases, when I call the function second time, it works.

              btw, the version is MODx Revolution 2.0.8-pl (traditional).
                • 22303 MODX Staff
                • 10,725 Posts
                I don’t know why this would happen unless anonymous users do not have permission to the Context or Resource until it is cached for some reason... huh
                  • 7966
                  • 90 Posts
                  Quote from: OpenGeek at May 12, 2011, 11:23 AM

                  I don’t know why this would happen unless anonymous users do not have permission to the Context or Resource until it is cached for some reason... huh

                  There is no anonymous user. I login to manager, call the function from a component. I don’t know if that calling is also an anonymous call in nature. It seems that if document is not cached yet by MODx, the function don’t work.
                    • 22303 MODX Staff
                    • 10,725 Posts
                    You need to provide more information about what you are precisely doing or it’s not going to be possible to help...
                      • 7966
                      • 90 Posts
                      Quote from: OpenGeek at May 13, 2011, 07:43 AM

                      You need to provide more information about what you are precisely doing or it’s not going to be possible to help...

                      Below are the shortened version of codes for the component,

                      each resource is flagged as published, since the file_get_contents was not working otherwise

                      $resources = $modx->getCollection('modResource',$c);
                      foreach ($resources as $resource) {
                         $resource->set('published','1');
                         $resource->save();
                      }




                      Loop through the resources
                      $resources1 = $modx->getCollection('modResource',$c1);
                      $table = $modx->getFullTableName("site_content");
                      
                      foreach ($resources1 as $resource1) {
                          $parent_alias = $modx->db->getValue('SELECT alias FROM '.$table.' WHERE id='.$resource1->get('parent'));
                            $output = $output.CreateHTMLFile_2($modx->makeUrl($resource1->get('id'),null,null,'full'), $resource1->get('alias'),  $parent_alias, $resource1->get('isfolder'));
                         $resource1->set('published','0');
                         $resource1->save();
                        
                      }
                      



                      and the function to get the html code and save it into a file.
                      function CreateHTMLFile($url, $alias, $parent_alias, $isfolder){
                      
                         $output = '';
                         $content = file_get_contents($url);
                         
                         if ($isfolder == '1') {
                              $filepath= $alias."/".$alias.".html";
                         }
                         else{
                      	    $filepath= $parent_alias."/".$alias.".html";
                         }
                         
                         $FileName = "/var/www/html/ab/zona/kmc/".$filepath;
                         $FileHandle = fopen($FileName, 'w') or die("can't open file");
                         fwrite($FileHandle, $content);
                         fclose($FileHandle);
                         $output =$output."--".$alias."--".$url."-Content length:".strlen($content)."******";
                           
                         return $output;
                      
                      }



                      Let me know if anything else I can provide.