Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use relative paths in exec command?

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 the PHP script. For example exec('something/program_name ......'); doesn't work if php script is not in the 'something' directory.

like image 384
mm. Avatar asked Dec 31 '25 18:12

mm.


2 Answers

You can use realpath to turn relative path into an absolute one before calling exec()

$rel = 'something/program_name';
$abs = realpath($rel);
exec($abs);
like image 104
GZipp Avatar answered Jan 03 '26 10:01

GZipp


Make it absolute, relative paths are evil.

exec(dirname(__FILE__) . 'program_name ......');
like image 34
Mike B Avatar answered Jan 03 '26 12:01

Mike B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!