Announcement

Collapse
No announcement yet.

fopen not working for remote files :(

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • fopen not working for remote files :(

    I'm writing a script that would parse remote files, however for some reason the fopen function doesn't work on remote files.. it will only returns attribute (like r, or rb)..

    I'm testing my script under WinNT 4.0 sp6, PHP4.06 / Apache1.3.20 / ActivePerl5.6 / MySQL3.23



    here is the part if my which doesn't work the file testdb.txt exists.. but it doesn't matter,even if the file doesn't exist or is at another server it just won't work.. only local files as long as i don't add http://

    $filename = "http://127.0.0.1/testdb/test.txt";

    $fd = @fopen($filename ,"r");

    if (!$fd) {
    $error.="Unable to open file: $filename.<BR>\n";
    }
    else{
    $contents = fread ($fd, filesize ($filename));
    $file = split("\n", $contents);
    fclose ($fd);

    echo "<PRE>";
    print_r($file);
    echo "</PRE>";
    }

  • #2
    Try fopen without the @...

    I have code something like what your doing, and it looks right... The only differnces is I don't use an @...

    Comment


    • #3
      I tryed it without the @ so that hopefully i would see the error message it is suppost to print.. but it doesn't. ;(..

      it makes no difference

      Comment


      • #4
        I've fixed it now by using file() instead of fopen(), and it works, however the fopen won't work, not on my local server or on another commercial server i tryed. It just wouldn't accept remote files..

        Comment


        • #5
          Is PHP running in safemode?

          Comment


          • #6
            There is a php.ini setting that will disable remote opening. You would have to look through the file for the name as it escapes me at the moment.

            Comment


            • #7
              My php.ini has the following 2 lines in it:

              safe_mode = Off
              and
              allow_url_fopen = On

              Comment


              • #8
                if you are using fopen on a local machine then you don't need the 127.0.0.1 or the http. also if you try to get it off another server while using your local 127.0.0.1 you won't as well. apache, if setup like this, won't access the outside world.

                Comment


                • #9
                  Try this...

                  PHP Code:
                  $filename "http://127.0.0.1/testdb/test.txt";

                  $fd = @fopen($filename ,"r") or die("Couldn't open file: $filename");

                  $contents "";
                  while (
                  $chunk fread($fd4096)) {
                      
                  $contents .= $chunk;
                  }
                  $file split("\n"$contents);
                  fclose ($fd);

                  echo 
                  "<PRE>";
                  print_r($file);
                  echo 
                  "</PRE>"

                  Comment

                  widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                  Working...
                  X