$fileID = fopen('myfile.txt', 'r'); - Fails when attempting to find file in same directory as script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    #1

    $fileID = fopen('myfile.txt', 'r'); - Fails when attempting to find file in same directory as script

    Using Linux/PHP 4.3.2 CLI:

    [PHP]$fileID = fopen('myfile.t xt', 'r'); // WORKS JUST FINE [/PHP]

    Using Linux/PHP 4.3.8 CLI:

    [PHP]$fileID = fopen('myfile.t xt'); // THROWS WARNING 'no such file or
    directory "myfile.txt " [/PHP]

    Using Windows XP/PHP 5.0.4 CLI:

    [PHP]$fileID = fopen('myfile.t xt'); // THROWS WARNING 'no such file or
    directory "myfile.txt "[/PHP]

    In all three cases this script, let's call it myscript.php, resides in
    the exact same directory as "myfile.txt ". Both scripts have
    permissions of 0644, same owner, same group (Unix).

    For the Windows problem I managed to use a coding solution that will
    use the absolute path that is generated within $_SERVER['PHP_SELF']
    (which apparently only occurs within Windows - have never seen that
    variable populated when using a UNIX-based OS):

    [PHP]
    if ($_ENV['windir'] || $_SERVER['windir']) {
    $path = substr($_SERVER['PHP_SELF'], 0,
    strrpos($_SERVE R['PHP_SELF'], '/') + 1);
    } else {
    $path = '';
    }
    [/PHP]

    This, however, will produce no absolute path information for UNIX-based
    OS using "myscript.p hp" to access "myfile.txt ". So using older
    versions of PHP as recent as PHP 4.3.8 within UNIX (Linux), "$fileID =
    fopen('myfile.t xt', 'r');" you have to extract from $argv[0], provided
    you are using CLI PHP and you just happen to NOT be in the directory
    where "myscript.p hp" is housed, otherwise, you obviously get no
    absolute path info to locate "myfile.txt ", and yes, when tested in PHP
    4.3.11 and PHP 5.0.4, I constantly got the warning "No such file or
    directory: "myfile.txt "" even though it's clearly there with
    myscript.php.

    Bottom line: Is there a foolproof way of always obtaining the absolute
    path of something using CLI PHP; or... what other alternatives have you
    all come up with using CLI PHP to always reliably locate a file that
    happens to be in the same directory as your script?

    Thanx
    Phil

  • Alvaro G Vicario

    #2
    Re: $fileID = fopen('myfile.t xt', 'r'); - Fails when attempting to find file in same directory as script

    *** comp.lang.php wrote/escribió (11 Aug 2005 09:20:22 -0700):[color=blue]
    > Bottom line: Is there a foolproof way of always obtaining the absolute
    > path of something using CLI PHP[/color]

    I think __FILE__ should work.


    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- http://bits.demogracia.com - Mi sitio sobre programación web
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    • comp.lang.php

      #3
      Re: $fileID = fopen('myfile.t xt', 'r'); - Fails when attempting to find file in same directory as script

      WOW! Thanx that was dead on perfect!
      Phil

      Comment

      • drwxr-xr-x

        #4
        Re: $fileID = fopen('myfile.t xt', 'r'); - Fails when attempting to find file in same directory as script

        On 12 Aug 2005 08:25:06 -0700, comp.lang.php wrote:[color=blue]
        > WOW! Thanx that was dead on perfect!
        > Phil[/color]

        Just _what_ are you nattering on about?
        Oh, I see the problem:
        X-Trace: posting.google. com


        DRAGON222 menjadi pusat resmi terunggul Sportsbook Digital. Sistem cepat respon, tampilan sangat lancar anti lag, dan kemungkinan menang terjadi pada tekad kuat anda.

        Comment

        Working...