How do I use relative paths in exec command?

How can I run an external program from PHP with a command exec

using relative paths?

 <?php

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

 ?>

      

This only works if it program_name.exe

is in the same directory as the PHP script. For example, it exec('something/program_name ......');

doesn't work if the php script is not in the directory something.

+2


source to share


2 answers


You can use realpath

to turn relative path to absolute before callingexec()



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

      

+7


source


Make it absolute, relative paths are evil.



exec(dirname(__FILE__) . 'program_name ......');

      

+3


source







All Articles