and file open was supposed to be so simple....

$DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];

$fp = fopen("/usr/home/digital/htdocs/test/text.txt", "r");

echo $DOCUMENT_ROOT;
echo ("<p>");
echo $fp;

Results in the folllowing being echoed to the browser:

/usr/home/digital/htdocs

Resource id #2

I have a simple text file am trying to output. Linux/apache. It is called text.txt in the /test directory.

    $fp is a file pointer.... a resource ID

    you want [man]fread[/man] or [man]file_get_contents[/man]

    links from those 2 will also give you some other posibilities.... but if you are only looking to display the contents, fread is the hardest way...

      Tried the fread() - as the get_file_contents() may have space issues:

      $filename = "test.txt";
      $handle = fopen($filename, "r");
      $contents = fread($handle, filesize($filename));
      echo $contents;
      fclose($handle);
      

      results in:

      Warning: fopen(test.txt): failed to open stream: No such file or directory in /usr/home/digital/htdocs/test/open.php on line 3

      Warning: filesize(): Stat failed for test.txt (errno=2 - No such file or directory) in /usr/home/digital/htdocs/test/open.php on line 4

      Warning: fread(): supplied argument is not a valid stream resource in /usr/home/digital/htdocs/test/open.php on line 4

      Warning: fclose(): supplied argument is not a valid stream resource in /usr/home/digital/htdocs/test/open.php on line 6

      I just looked yet again, and the text file is there, named exact w/text in it. Damn, wasting another day on a simple BS thing.

        Hi,

        Hm.. You see the error? See what it sais?

        In general, it is good practice to solve one error at a time, starting with the first, since the second, third, .. error may be th eresult of hte first.

        The first error in htis case is

        Warning: fopen(test.txt): failed to open stream: No such file or directory in /usr/home/digital/htdocs/test/open.php on line 3

        Basically telling you that htere is no file called test.txt in the directory. The directory it looks for it, is the same as the script is in, unless you define a path.

        So now it has looked for:

        /usr/home/digital/htdocs/test/test.txt

        Please make sure the file is there

          Which compounds the oddity - its there.

          Am looking at it now. Spelling is correct, case is same. And a file_exists() shows the files not there, even though it is.

            Save the contents of the file locally, and test with "w+" instead of "r".
            After that, switch back to "r" and see what happens.

              Danmed typos. Working well now with the correct spelling on the filename.

              Its always something simple. Had to walk away from it for a few minutes.

              Thanks to all. Learned something new today.

                To simplfy it all - further study provided this as an efficient means:

                readfile('/usr/home/digital/htdocs/test/text.html');
                

                Does it all - it even echos the file out. Hope this saves someone some time.

                  Write a Reply...