Re: Trapping failure of file_get_contents()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Marshall,

the function file_get_contents may have problem with large files.

You could try get the file using "fread" and feof.

Here i put a example



$contents = '';
while(!feof($stream)){
    $contents .= fread($stream, 8192);
}


Gerardo Benitez.





On Thu, Oct 22, 2009 at 12:38 PM, Marshall Burns <ListMail2@xxxxxxxxxx>wrote:

> I have a script that downloads a sequence of files online. Every hundred
> files or so, it fails with:
>
>
>
> ==========================
>
> Warning: file_get_contents(<URL>) []: failed to open stream: A connection
> attempt failed because the connected party did not properly respond after a
> period of time, or established connection failed because connected host has
> failed to respond. in <script>.php on line <number>
>
> Fatal error: Maximum execution time of 30 seconds exceeded in <script>.php
> on line <number>
>
> ==========================
>
>
>
> I have tried to trap this so that I could loop back and let the script try
> the access again. Failing that, I've also tried to get a shutdown function
> to give me a restart link. The code for this is:
>
>
>
> ==========================
>
> $bWorking = true;
>
>
>
>            .
>
>            $bDone = false;
>
>            while(! $bDone)
>
>               {$sFil = file_get_contents($sURL);
>
>                        if($sFil)
>
>                           {$sFileSize = file_put_contents('image/' . $sID .
> '.jpg', $sFil);
>
>                                    $bDone = true;}
>
>                          else
>
>                           {echo('<br>DL error on ' . $sURL);
>
>                                    $sFileSize = 'DL error';
>
>                                    set_time_limit(30);}
>
>                        echo(', got ' . $sFileSize);
>
>                        }
>
>            .
>
>
>
> $bWorking = false;
>
>
>
> register_shutdown_function('ReadFileTimeout');
>
> function ReadFileTimeout()
>
>            // Provide a link to restart processing after a timeout in
> file_get_contents().
>
>   {global $bWorking, $sParameters;
>
>            if($bWorking)
>
>                        echo('<p><a HRef="<script>.php?parameters=' .
> $sParameters . '" target="_blank">Restart</a>');
>
>            }
>
> ==========================
>
>
>
> None of that is working. I guess the loop doesn't work because the warning
> is emitted after the script has already been aborted by the timeout. But in
> that case, the shutdown function should kick in, but the link is not being
> output.
>
>
>
> So two questions:
>
>            (a) Is there a way to get file_get_contents() to stop trying
> before the script time limit is reached, so that my loop could work?
>
>            (b) Why is the shutdown function not working?
>
>
>
> Thanks for your help.
>
>
>
> Marshall
>
>
>
>
>
>
>
>
>
>
>
>


-- 
Gerardo Benitez

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux