Jump to content

Unable to open or read from text file via command line php.exe


Niloreptiso
Go to solution Solved by Ch0cu3r,

Recommended Posts

I have a PHP file that is executed via batch file very frequently for live updating. This is a sort of "sync" file for reading/writing to a MySQL database. I am able to get it functioning absolutely fine when executed manually via a web browser (and my echo debug lines output the expected information), yet when I run the same PHP file via the command line, it seems to just not be capable of opening any file for reading, so the equivalent variables that are correct when executed on a web browser are blank when echoed through the command line.

Is there a different way of handling reading of files when running a php script via the command line, or should it function exactly the same as when run via a browser?

For instance:

$k = "0";
$line = file("examplefile.log")[$k];
echo $line;

The above will output the first line (line 0) of examplefile.log when run via a browser, yet in the command line it doesn't echo anything.. as if it's not able to read the text file for some reason even though other parts of the php file run fine.

 

In my actual code, I am using a foreach loop to run through each line of a file (very few lines and very small file) that then explodes each line and sends the necessary parts to a mysql database; this works fine when I run the php file (and hence script) through the browser, but doesn't do anything within the foreach loop when it runs via the command line php.exe.

Edited by Niloreptiso
Link to comment
Share on other sites

  • Solution

 

 

 there a different way of handling reading of files when running a php script via the command line, or should it function exactly the same as when run via a browser?

There will be a difference yes. When ran from the browser file paths will be relative to your document root. But with the command line files paths will be relative from where the command was executed from.

 

Try setting the full file path to examplefile.log in your PHP script.

Link to comment
Share on other sites

There will be a difference yes. When ran from the browser file paths will be relative to your document root. But with the command line files paths will be relative from where the command was executed from.

 

Try setting the full file path to examplefile.log in your PHP script.

 

aha.. the only thing I hadn't thought of! I had tried everything else (and lost some hair along the way) but it really is that simple. Thanks very much :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.