PHP Warning: fopen ... Trying to run from root, file is present, but can't be found?

Discussion in 'Server Operation' started by bpmee, Aug 30, 2006.

  1. bpmee

    bpmee Member

    Hi,

    Long time forum reader, first time poster :)

    I'd be really grateful for any help on this error:

    When I attempt to execute the following command from CRON or when logged in as the ROOT,

    [root@asdasd ~]# /usr/bin/php /var/www/web12/web/qazm/abccode.php

    from shell I get an error:

    PHP Warning: fopen(my-map.txt): failed to open stream: No such file or directory in /var/www/web12/web/qazm/abccode.php on line 3

    Here is the php script that calls the file:

    The file "my-map.txt" is located in my "web" folder, the root directory..
    PHP:
    <?php
    $blog_name 
    "Blog";
    require 
    'weblog.php';
    $handle fopen($_SERVER["DOCUMENT_ROOT"]."/my-map.txt""r");
    BUT, the file, "my-map.txt" IS IN THE Folder! It is definitely there, I've checked numerous times!

    I have tried:

    1.) Un-checking SUexec in the server settings
    2.) Logging in via SSH and Chmoding the file my-map.txt to 777
    3.) Logging in via SSH and Chmoding the file's folder and web user folder to 777.
    3.) Playing around with Apache config, document root, etc.
    4.) Playing around with PHP.ini ..

    but, no Luck!:confused:

    NOTE, I CAN run the script from my web browser, or if I type out the absolute path to the file... but, If I use the $_SERVER["DOCUMENT_ROOT"] variable or, use a path like: ../my-map.txt from the ROOT or CRON I get the same "file cannot be found" etc error...

    Is this a config or permission issue?

    Thanks for any help in advance...
     
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    The variable $_SERVER["DOCUMENT_ROOT"] is only defined when you run a php script in a webserver. If the script is run on the shell, this variable does not exist. Please try the following code:

    I assume that my-map.txt is in the smae directory then the file abccode.php
     
  3. bpmee

    bpmee Member

    Thanks for your reply! But, no luck!

    Hi Till,

    Thanks so much for your reply, I really appreciate it.

    I tried the code that you suggested, without the $_SERVER["DOCUMENT_ROOT"] variable...

    Note the following script is named abccode.php in the SAME directory as "my-map.txt"...

    PHP:
    <?php
    $weblog_name 
    "Blog";
    require 
    'weblog.php';
    $handle fopen("my-map.txt""r");
    ### Rest of code ###
    ?>
    ...and when I ran the script from CRON or as ROOT from the Command Line, I got the same errors as before:

    (Permissions on "my-map.txt" set to 777)
    [root@xxxxxx8 ~]# /usr/bin/php /var/www/web12/web/qazm/abccode.php
    PHP Warning: fopen(my-map.txt): failed to open stream: No such file or directory in /var/www/web12/web/qazm/abccode.php on line 4

    (Permissions on "my-map.txt" set to 644)
    [root@xxxxxx8 ~]# /usr/bin/php /var/www/web12/web/qazm/abccode.php
    PHP Warning: fopen(my-map.txt): failed to open stream: No such file or directory in /var/www/web12/web/qazm/abccode.php on line 4

    I'm using ISPconfig (of course :) ) on a Fedora OS...

    Anyone have a thought? I am stumped....:confused:
     
  4. Ben

    Ben Active Member Moderator

    How does the cron call look like?

    what you can do is, store your basedir and append it to the filename:

    PHP:
    $handle fopendirname(__FILE__) . '/my-map.txt''r');
    If it still does not work please add this line for debugging before the fopenline
    PHP:
    var_dumpdirname(__FILE__) );
    var_dumpis_file(dirname(__FILE__) . '/my-map.txt') );
    var_dumpis_readable(dirname(__FILE__) . '/my-map.txt') );
    Next thing, when I get these errors right, you should do is some more errorhandling, because after the fopen your script seems not to exit.
    Either you add a die() in die fopen line or sth. like
    PHP:
    if( !is_reosource($handle) )
    {
       
    //Your errormessage and exit, die(), logging or whatever here
    }
     
    Last edited: Aug 30, 2006
  5. bpmee

    bpmee Member

    Thanks Ben - This solution worked!

    Hi Ben -

    Thanks, your solution worked! I was able to get the script to work from the command line AND from my browser.

    I'm assuming now that it will run from CRON without any trouble, but I will do a test run and see what it does!

    Again, thanks!
     
  6. Ben

    Ben Active Member Moderator

    I guess the path was missing when ran from cron so the file was not found, even if the script was in the same directory as the file.
     

Share This Page