douke7274 2015-11-02 18:51
浏览 69
已采纳

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  
  • 写回答

1条回答 默认 最新

  • dtrz17917 2015-11-02 19:30
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog