I'm trying to build a simple diagostic endpoint in Laravel to know what versions of software is installed on the queried machine. In this sense I have these lines of code with the corresponding outputs:
dump(shell_exec('pdftotext -v')); // null
exec('pdftotext -v', $output, $code);
dump($output, $code); // [], 0
But if I simply write the same thing in the terminal this works well:
> pdftohtml -v
pdftohtml version 21.12.0
Or even:
> php -r 'shell_exec("pdftotext -v");'
pdftohtml version 21.12.0
So then why is this difference and why can't I run the same thing from PHP?
Same behaviour on OSX locally and on a production Vapor environment.
My Answer:
pdftotext version 4.03
It seems like the issue might be related to the environment in which the `shell_exec()` function is running. When running commands in the terminal, the environment variables are set up correctly, but when running it through `shell_exec()` in PHP, the environment might be different.
One way to troubleshoot this issue is to specify the full path to the `pdftotext` executable in the `shell_exec()` function. For example:
php
$output = shell_exec('/usr/bin/pdftotext -v');
dump($output);
Make sure to replace `/usr/bin/pdftotext` with the actual path to the `pdftotext` executable on your system.
Additionally, you can try setting the PATH environment variable explicitly before running the command:
php
putenv('PATH=' . getenv('PATH') . ':/usr/bin');
$output = shell_exec('pdftotext -v');
dump($output);
This will ensure that the `pdftotext` executable can be found in the PATH when running the command through `shell_exec()`.
If the issue persists, you may need to check the permissions of the `pdftotext` executable and the user running the PHP script to ensure that the executable can be executed by the PHP process.
Rate this post
3 of 5 based on 5867 votesComments