tags:

views:

226

answers:

2

how to run external program from php with exec command using relative paths?

 <?php

  exec('program_name ......');

 ?>

this works only if program_name.exe is in the same directory as this php script. for example

   exec('something/program_name ......');

doesnt work if php script is not in the 'something' directory. anybody know how use relative paths in exec command ? thanks

+3  A: 

Make it absolute, relative paths are evil.

exec(dirname(__FILE__) . 'program_name ......');
Mike B
Will work if "program_name" is in the same directory as the script, but not if, as in op's case, "php script is not in the 'something' directory".
GZipp
@GZipp, author did not give enough information either way. What is his path relative to? The PHP script or some other include path that wasn't specified? I gave a general answer to a general question.
Mike B
But your answer seems more specific than general to me. It applies to paths within the current file's path, but not to relative paths that are outside of that. In any event, I merely pointed out one case where your answer will work, and one (general) case where it won't.
GZipp
A: 

To answer your question, "how [to] use relative paths in exec command?"

$rel = 'something/program_name';
$abs = realpath($rel);
exec($abs);
GZipp