skip to Main Content

The following code only works in my Terminal but it doesn’t work in my localhost xampp linux Unbuntu 20.04, Both shell_exec() and exec() don’t work. When I go to localhot it executes all the code but not its two functions. The following code does not execute.

// Add the --enable-local-file-access option to enable access to local files (HTML)
$wkhtmltopdfCommand = "wkhtmltopdf --enable-local-file-access {$outputHtmlPath} {$pdfOutputPath}";
shell_exec($wkhtmltopdfCommand);

I don’t know if I need to change anything for this to work, but I need some advice.

2

Answers


  1. 1) Check the security setup

    Did you check and compare the php.ini settings between web and CLI?

    In your script, add this at the beginning:

    phpinfo();
    die();
    

    Run your page and check the settings applied at execution. You’ll
    also see which php.ini files are read and applied.

    The problem is that shell_exec() and exec() are probably disabled
    for security reasons. This can be changed by editing
    PHP’s disable_functions option.

    INI settings are usually stored in specific *.ini files under the
    /etc/php/* directories. So you have to look for some settings
    in there. Typically, if you have PHP 8.2 running over FPM then
    you have to check /etc/php/8.2/fpm/php.ini.

    My /etc/php/8.2/cli/php.ini file has disable_functions set to an
    empty string, meaning that in command line there’s no restriction.
    But in my FPM settings, I have a huge list of critical functions
    which are disabled to reduce the risk of having my server hacked.

    Apache configuration can also set some
    PHP settings
    ,
    just for a specific Virtual Host. So have a look at them if you can’t
    find something in the php.ini files.

    Depending on your PHP setup, you may also have to check
    safe_mode
    and
    suhosin.executor.* if you have the Suhosin hardening module installed.

    2) Use a full path to the binary

    When shell_exec() is run from the web server, I think that
    it’s rather common that the binary isn’t found in the PATH.

    Open a console and run

    which wkhtmltopdf
    

    This should output the full path to the binary. Certainly something
    such as /usr/bin/wkhtmltopdf.

    Now replace your PHP code with the full path to the binary:

    // Add the --enable-local-file-access option to enable access to local files (HTML)
    $wkhtmltopdfCommand = "/usr/bin/wkhtmltopdf --enable-local-file-access {$outputHtmlPath} {$pdfOutputPath}";
    shell_exec($wkhtmltopdfCommand);
    
    Login or Signup to reply.
  2. Instead of

    shell_exec($wkhtmltopdfCommand);
    

    try

    $result = passthru($wkhtmltopdfCommand . ' 2>&1');
    var_dump($result);
    

    and it should tell you what’s wrong.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search