Niloreptiso Posted September 26, 2014 Share Posted September 26, 2014 (edited) 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 September 26, 2014 by Niloreptiso Quote Link to comment https://forums.phpfreaks.com/topic/291308-unable-to-open-or-read-from-text-file-via-command-line-phpexe/ Share on other sites More sharing options...
Solution Ch0cu3r Posted September 26, 2014 Solution Share Posted September 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291308-unable-to-open-or-read-from-text-file-via-command-line-phpexe/#findComment-1492197 Share on other sites More sharing options...
Niloreptiso Posted September 27, 2014 Author Share Posted September 27, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291308-unable-to-open-or-read-from-text-file-via-command-line-phpexe/#findComment-1492211 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.