phpshellexecpngquant

Is this escaped shell command wrong


Here is my escaped shell command which I run through exec command in PHP:

C:^\Program Files^\pngquant^\pngquant.EXE \"--force\" \"--ext=.png\" \"--\" \"F:\laragon\www\demo\images\uploads\43d67cba7a951378485882e5b515c825943f7d0a.png\"

I get return code 1 (i.e. error). On the other hand, if I run pngquant "--force" "--ext=.png" "--" "F:\laragon\www\demo\images\uploads\43d67cba7a951378485882e5b515c825943f7d0a.png"directly in shell, it works fine.

Is there something wrong with the way I am escaping command and arguments.

Edit 1: Here is my PHP exec command:

escapeshellcmd($this->cmd).' '.implode(' ', array_map('escapeshellarg', $args))

Where $this->cmd is C:\Program Files\pngquant\pngquant.EXE and print_r($args) is

Array(    
    [0] => "--force",
    [1] => "--ext=.png",
    [2] => "--",
    [3] => "F:\laragon\www\demo\images\uploads\d3a2966596a94343b5a732368dcb79394a8d4d69.png"
)

Solution

  • Doing escapeshellarg($this->cmd).' '.implode(' ', array_map('escapeshellarg', $args)) cleared the error and image was also compressed. It's strange that escapeshellcmd puts control character(^) before \.