I have used and tested a script on my linux machine with an apache webserver and php4 installed, when i uploaded the script i got a error of a bad file desctiptor in the fopen statement:

The code i used is:

$webpage="http://www.zuid.knkv.nl/file.htm";
$fp=fopen($webpage,"r") or die("Error");

when the url was http://localhost/blabla everthing worked fine!

Who can help me????
Regards,
Mark

    No. fopen does not fail on 404s.
    It simply opens a filepointer to the 404 page, because that is what the webserver is sending.

    fopen only fails if the location it tries to open it not accessible: network down, location does not resolve in the DNS, remote server refuses connection, etc.)

      No its not a 404, the URL is a fictive URL.

      The problem only occurs when a use a URL instat of a PATH. It works fine with /home/httd/html/....... and also with http://localhost/... on my own machine.

      Maybe i have compiled php4 different then my ISP??

      Thanks,
      Mark

        ... No it is a 404.

        /Mike

          2 months later

          I am experiencing the same problem:

          <?
          $fp = fopen("http://www.bbc.co.uk","r");
          $file = fread($fp, 10000);
          fclose($fp);
          echo "Success!";
          ?>

          Returns this:

          Warning: fopen("http://www.bbc.co.uk/","r") - Bad file descriptor in /home/public_html/test.php on line 2

          Warning: Supplied argument is not a valid File-Handle resource in /home/public_html/test.php on line 3

          Warning: Supplied argument is not a valid File-Handle resource in /home/public_html/test.php on line 4
          Success!

          However, if I use http://www.php.net/, I receive no warnings. And, this only occurs on the production server. All is well on my local machine.

          Help appreciated. Oh, and to avoid debate the URLs will not result in a 404. ;-)

            Vincent, you are wrong. A HTTP status 404 results in
            1. a php warning
            2. a FALSE return
            when doing a fopen() on the URL.

              2 months later

              I too am trying to open files on some other host (unsuccesfully I should add 😉 ).
              But aren't we suppose to use fsockopen() for this purpose?

                • [deleted]

                Are you trying to read html documents from the remote server or are you trying to read a textfile from the remote server's harddisk?

                  I was trying to read a html document from a remote server.

                  Fortunately I found what my problem was. The company I work for has installed a pretty airtight proxy server that didn't allow a socket connect. When I tried a document inside our network everything went perfectly.

                    whoops:

                    $fp = fsockopen ("www.php.net", 80, $errno, $errstr, 30);
                    if (!$fp) {
                    echo "$errstr ($errno)<br>\n";
                    } else {
                    fputs ($fp, "GET / HTTP/1.0\r\n\r\n");
                    while (!feof($fp)) {
                    $contents .= fgets ($fp,128);
                    }
                    fclose ($fp);
                    }

                    works as illustrated at PHP.net

                      10 months later

                      I am having the same problem. I am using the following code...

                      <?php
                      $string = @fopen("http://www.playboating.com/hurley.htm", "r") or die("Unable to reach Gate Gauge");

                      while(!feof ($string)) {

                      $buffer=$buffer.fgets($string, 4096);

                      }

                      fclose ($string);

                      ?>

                      It works fine when run from my local version of apache, however it gives similar errors to MArtins when run of of nsdesign (my webhosts) servers!

                      Any help would be great!

                      Jon

                        2 months later

                        Bad file descriptor (9)

                        still can't work

                        I'm using apache on windows98, and squid run on the server.

                        I think it must be run from a same level domain to get it work.

                        webserver:

                        <mycompany>-------<other>
                        |
                        |
                        |


                        | | |
                        Winnt Linux unix
                        |
                        Mycomputer

                        That is only work when i request to any server below mycompany, but never work when request set to other server on the internet.
                        why ?

                          2 months later

                          I get the same error when there is a redirect on the target URL.

                          Without the redirector, everything works fine.

                          Anybody have any hints, how to go around this problem ?

                          Thank you.

                            Write a Reply...