|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22730 fopen/fsockopen opens local file instead of doing a HTTP request
Submitted: 2003-03-15 11:55 UTC Modified: 2003-03-15 23:08 UTC
From: web at affenkrieger dot de Assigned:
Status: Not a bug Package: Network related
PHP Version: 4.3.1 OS: Windows 2000
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: web at affenkrieger dot de
New email:
PHP Version: OS:


 [2003-03-15 11:55 UTC] web at affenkrieger dot de
I'm runnin' a local Apache webserver 1.3.27 on my win2000 machine. PHP 4.3.1 is included as a CGI.
When i'm trying to access a non-existing file on my local Apache, PHP tries to access it as a local file and isn't doin a HTTP request as it should do.

  fopen("http://localhost/404.html", "r");

PHP throws this msg into the error_log of Apache:
> PHP Fatal error:  Unknown(): Unable to open
> X:\webroot\404.html in Unknown on line 0
.. but no info to STDOUT

  fopen("", "r");
Just works as expected:
> PHP Warning:  fopen(
> failed to create stream: HTTP request failed! 
> HTTP/1.1 404 Not Found

But it becomes more curious. Doing a HTTP request on my own with fsockopen results in the same error as above!

    $fp = fsockopen ("localhost", 80, $errno, $errstr, 5);
    { die($errstr);}
    $request  = "GET /404.html HTTP/1.0\r\n";
    $request .= "User-Agent: PHP 4.3.1\r\n";
    $request .= "Host: localhost\r\n";
    $request .= "Connection: Close\r\n\r\n";
    $return = '';
    fputs ($fp, $request);
    $line = fgets ($fp, 512);
    $this->header["status"] = $line;
    while (!feof($fp))
        $line = fread ( $fp, 1024 );
        $this->content .= $line;
        $line = fgets ($fp, 128);
        if($line == "\r\n")
        { $header_done=true;}
          $data = explode(":",$line);
          $this->header[$data[0]] = trim($data[1]);
    fclose ($fp);

I am sending a HTTP GET-request to localhost to get the non-existing file, but the answer of the Apache is "200 OK"! And then the same log entry as with fopen():
> PHP Fatal error:  Unknown(): Unable to open
> X:\webroot\404.html in Unknown on line 0

Why does PHP wants to open a local file, even if i'm doing a HTTP request manually ?
The same script works with an other host as expected, too.



Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-15 20:38 UTC]
My *guess* is that you have apache configured to handle .html files through PHP.  When you request '404.html' apache passes the request to the PHP engine (a second instance, not the instance which issued the request) which attempts to open the file specified via apache and subsequently doesn't find it.

Consider this.... what happens when you open a normal webbrowser (one your local box) and browse http://localhost/404.html ?

 [2003-03-15 23:08 UTC]
Support questions elsewhere, thank you.

PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Sep 27 18:05:57 2022 UTC