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

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  

我正在使用imagemagick和php来处理一些单词。 我首先将一个句子分成单词,然后为我打算通过PHP的exec()命令调用的长命令行参数准备这些单词。 我仔细检查过这个论点; 根据我的知识,所有字符都被正确转义,包括单引号和双引号。 exec()函数不起作用,说“文件名,目录名或卷标语法不正确” code>。 但是,当我回显 $ escaped变量 code>并将回显的字符串分配给php的exec()时,它的工作没有问题。 p>

这是回显的字符串 p>

  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“);  //它工作
  code>  pre> 
 
 

我正在使用的代码: p>

  $ 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。  '+追加'。  $名称。  '.PNG';  
 
功能w32escapeshellarg($ s)
 {return'“'。addcslashes($ s,'\\”')。  ''';}} 
 
 $ escaped = w32escapeshellarg($ command); 
exec($ escaped); //它无法正常工作
  code>  pre> 
  div>

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);