PHP exec()命令不接受有效的变量执行

I am using imagemagick and php together to process some words. I first break a sentence into words, then prepare those words for a long command line argument which I plan to call via PHP's exec() command. I have double-checked the argument; all characters are properly escaped including single and double quotes as per my knowledge. The exec() function does not work, saying "The filename, directory name, or volume label syntax is incorrect". But when I echo the $escaped variable and assign the echoed string to the php's exec(), it works without problem.

Here is the echoed string

exec("convert -background DeepSkyBlue -fill black -font Ultima-Alt-Bold.ttf -pointsize 90 -gravity center -density 90 label:\"SALIVA \" -fill black -font Ultima-Alt-Bold.ttf -pointsize 90 -gravity center -density 90 label:\"USED \" -fill black -font Ultima-Alt-Bold.ttf -pointsize 90 -gravity center -density 90 label:\"AS! \" +append Ulti.png"); // It works 

The code I am using:

$file = 'theboldfont.ttf';
$name = substr($file, 0, 4);

$s = "SALIVA USED AS!";

$words = explode(' ',$s);
$string = '';

foreach ($words as $word) 
{
    $string .= " " . '-fill black -font ' . $file . ' -pointsize 90 -gravity center -density 90 label:"' . $word . ' "';  
}

$command = 'convert -background DeepSkyBlue ' . $string . ' +append ' . $name. '.png';  

function w32escapeshellarg($s)
{ return '"' . addcslashes($s, '\\"') . '"'; }

$escaped = w32escapeshellarg($command); 
exec($escaped); // It is not working  

Use escapeshellarg to escape your strings:

<?php
$file = 'theboldfont.ttf';
$name = substr($file, 0, 4);

$s = "SALIVA USED AS!";

$words = explode(' ', $s);
$string = '';

foreach ($words as $word) {
    $string .= ' -fill black -font ' . escapeshellarg($file) . ' -pointsize 90 -gravity center -density 90 label:' . escapeshellarg($word);
}

$command = 'convert -background DeepSkyBlue ' . $string . ' +append ' . escapeshellarg($name . '.png');

exec($command);