Search code examples
phpcakephpreflectionfile-get-contentsfwrite

Writing files to webroot in a loop, "Fatal error: ReflectionClass::getProperties()"


I am calling this function from my browser through a controller:

function writePages($startPage)
{
    for ($page=$startPage;$page<90;$page++)
    {
        $content=file_get_contents('http://www.websiteIamUsing.com/'.$page);
        $handle=fopen('mytxtfile'.$page.'.txt','w');
        fwrite($handle,$content);
        fclose($handle);
    }
}

For some reason, this will write the first 20-something, or sometimes 50-something files, but then it will stop and give me this error:

Warning: get_class() expects parameter 1 to be object, resource given in 
/Applications/MAMP/htdocs/mysite/lib/Cake/Utility/Debugger.php on line 579

Warning: get_object_vars() expects parameter 1 to be object, resource given in 
/Applications/MAMP/htdocs/mysite/lib/Cake/Utility/Debugger.php on line 585

Warning: Invalid argument supplied for foreach() in 
/Applications/MAMP/htdocs/mysite/lib/Cake/Utility/Debugger.php on line 586

Warning: ReflectionObject::__construct() expects parameter 1 to be object, resource 
given in /Applications/MAMP/mysite/alexmohamed/lib/Cake/Utility/Debugger.php 
on line 592

Fatal error: ReflectionClass::getProperties() 
[<a href='http://php.net/reflectionclass.getproperties'>reflectionclass.getproperties</a>]: 
Internal error: Failed to retrieve the reflection object in 
/Applications/MAMP/htdocs/mysite/lib/Cake/Utility/Debugger.php on line 594

Can anyone explain to me what is going on here? Since it begins to work, I suspect maybe CakePHP doesn't let you run a script for longer than a certain amount of time or something? Is there a way around this? This is a controller action that only I will use...it won't be used by the users of the site so it can take a long time (this one should take about a minute).

Edit: If you look at the errors, it appears to be trying to debug something, but since cakePHP is passing a "resource" rather than an "object" to the debugger, it won't tell me the real error. Why would this be an unreliable action...sometimes it completes until the end, sometimes it stops after ~20 files, sometimes after ~50...


Solution

  • Thanks everyone...both suggestions were helpful. I upgraded to 2.4.1 and Cake didn't send a resource to the debugger anymore, so I was able to see the real error which was simply that file_get_contents failed. So I changed the for loop into a while loop like so:

    $page=1;
    $pages=90;
    while ($page<=$pages)
    {
        if($content=file_get_contents('http://www.websiteIamUsing.com/'.$page))
        {
            $handle=fopen('mytxtfile'.$page.'.txt','w');
            fwrite($handle,$content);
            fclose($handle);
            $page++;
        }
    }
    

    There are still errors because file_get_contents fails randomly every so often, but then it will keep retrying until it works, and now even though there are errors, I always end up with the final result I wanted. If anyone can shed insight onto why this command is unreliable, let me know.