views:

1678

answers:

2

When executing the following block of code:

foreach($eventfiles as $eventfile)
{
 if($eventfile['filename'])
 { 
  $file = $eventfile['filepath'];
     // Open File
     if( !($fp = fopen($file, "r")))
     {
         echo '<br>CAN NOT READ FILE.';
         exit;
     }
     // Read data from the file into $data
     $data = "";
     while (!feof($fp)) $data .= fread($fp,1024);
     query("update event_rtab set html = '".escape($data)."' where id = {$eventfile[id]}");
 }
 if($eventfile['eventType']=='email')
 {
  query("INSERT INTO event_email_rtab (event_id,subject) values ($eventfile[id],'".escape($eventfile[email_subject])."')");
 }
}

The script fails with the following error:

fopen(test.html) [function.fopen]: failed to open stream: Redirection limit reached, aborting in /data/www/example.com/public/test.php on line 843

What causes this error and how can I correct it?

A: 

It's very much likely due to the file you're trying to open is trying to open ANOTHER file and thusly reaching the limit of redirection.

Edit: When I say likely, I mean that can cause the error and this may be that situation.

jerebear
+3  A: 

Where are the files located you are trying to open? Are they on the local filesystem or are you trying to access them via HTTP(S)?

If you're using some network protovol wrapper this error is most likely connected to too many (HTTP - in case of the HTTP(S) protocol) redirects on the way from your script to the file you want to open. The default redirection limit should be 20. As 20 redirects are quite alot there could be some error in the filename itself (causing e.g. the webserver on the other end to do some spellcheck-redirects) or the other server is misconfigured or there are some security measures in place or...

If you feel the need to extend the 20 redirects you could use a stream context.

$context = array(
    'http'=>array('max_redirects' => 99)
);
$context = stream_context_create($context);
// hand over the context to fopen()
$fp = fopen($file, 'r', false, $context);
// ...

Please see:

Stefan Gehrig