LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 12-09-2008, 09:23 AM   #1
asiandude
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
Code:
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:
Quote:
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
CDollmont
Member
 
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
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
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
asiandude
LQ Newbie
 
Registered: Dec 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
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
Quote:
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
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
I thought the correct expression is:
Code:
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:
Code:
-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
              names.
The excerpt is from the man page.

jlinkels
 
  


Reply


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


All times are GMT -5. The time now is 03:05 AM.

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