Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-09-2008, 09:23 AM   #1
LQ Newbie
Registered: Dec 2008
Posts: 6

Rep: Reputation: 0
php cli can't run php file outside its path

Hello People! I am new here I hope someone could help me with a scripting problem...

ok here goes..

I have many php files i want to run periodically using php cli and they are located all over the place so i decided to seek them out for execution.

This is what i intend to run in cron
find /home/ -type f -name "filename.php" -print | xargs php

the problem is my php files do not use full path so if i run them outside their path i will get the following error:
Warning: require_once(../../config.php): failed to open stream: No such file or directory in /home/somepath/where/thefile/located/filename.php on line 3
if i go to the path where the php files are located and type 'php filename.php' it will run just fine.

I do not want to change the path in all my php files. So is there a way to run my phpfiles as if i run them from their path?
Old 12-09-2008, 01:20 PM   #2
Registered: Feb 2006
Distribution: Ubuntu/OpenSuSE
Posts: 33

Rep: Reputation: 15
Instead of piping the find results to "xargs php" pipe them to "xargs mywrapper". In "mywrapper", trim the path off the filename, do a cd to that path, and execute the file there.
Old 12-09-2008, 10:09 PM   #3
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
As CDollmont has said you need to change the directory before executing. Another approach would be to change the include_path, this can be done by changing the php.ini file or on the fly with the set_include_path() function. This is probably of more interest in the future, where a line such as set_include_path(get_include_path() . PATH_SEPARATOR . getcwd()); in your main function may sort out this sort of problem for you.
Old 12-10-2008, 03:18 AM   #4
LQ Newbie
Registered: Dec 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Originally Posted by CDollmont View Post
Instead of piping the find results to "xargs php" pipe them to "xargs mywrapper". In "mywrapper", trim the path off the filename, do a cd to that path, and execute the file there.
Thanks guys... i tried to do a pipe xargs cd on find directory and got
find: `cd': No such file or directory

i am sure the output path is correct.. not sure why cd doesnt work.

pardon me for being a newbie.
Old 12-11-2008, 02:50 PM   #5
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,634

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
I thought the correct expression is:
find /home/ -type f -name "filename.php" -exec php {} \;
Additionally, when writing PHP you should make *no* assumptions as from where it is executed. Imagine you would execute 'ls' and it would complain it could not find the directory for its includes! (I know there aren't any)

You should specify the absolute path of the includes in your php source code.

This might help you as well:
-execdir command {} +
              Like -exec, but the specified command is run from the subdirectory  containing
              the  matched  file,  which  is not normally the directory in which you started
              find.  This a much more secure method for invoking commands, as it avoids race
              conditions  during  resolution of the paths to the matched files.  As with the
              -exec action, the ‘+’ form of -execdir will build a command  line  to  process
              more than one matched file, but any given invocation of command will only list
              files that exist in the same subdirectory.  If you use this option,  you  must
              ensure that your $PATH environment variable does not reference ‘.’; otherwise,
              an attacker can run any commands they like by leaving  an  appropriately-named
              file  in a directory in which you will run -execdir.  The same applies to hav‐
              ing entries in $PATH which are empty  or  which  are  not  absolute  directory
The excerpt is from the man page.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP won't recognize my Configuration File (php.ini) path kriezo Programming 4 02-14-2008 02:31 AM
Apache 2, PHP 5, SuSE 9.2 - cannot run php files dickohead Linux - Networking 6 06-28-2006 10:25 AM
php script will not run correctly as cron, fine from cli dtra Linux - Software 2 12-22-2005 07:21 AM
get full path of uploaded file in php spoody_goon Programming 2 05-10-2005 07:32 PM
help getting pptp-php-gtk.php to run as root mrtwice Linux - Software 0 11-21-2003 12:49 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration