如何在shell脚本中使用PHP变量?

如何在shell脚本中使用PHP变量?,php,shell,curl,Php,Shell,Curl,我想用PHP向远程服务器发送一个文件。当我将文件名硬编码时,以下代码可以正常工作: $output = shell_exec('curl -XPOST -F "file=@myfile.txt" http://135.195.42.168:6007'); echo "<pre>$output</pre>"; $output=shell\u exec('curl-XPOST-F“file=@myfile.txt”http://135.195.42.168:6007');

我想用PHP向远程服务器发送一个文件。当我将文件名硬编码时,以下代码可以正常工作:

$output = shell_exec('curl -XPOST -F "file=@myfile.txt" http://135.195.42.168:6007');
echo "<pre>$output</pre>";
$output=shell\u exec('curl-XPOST-F“file=@myfile.txt”http://135.195.42.168:6007');
回显“$output”;
现在我需要动态地决定“file=@myfile.txt”。我尝试了以下方法:

$filePath = $_SERVER['DOCUMENT_ROOT'].'package/myzip_'.$owner_id.'.zip';
$name = basename($filePath);
$content = "file=@{$name}";
$output = shell_exec('curl -XPOST -F ' . $content . ' http://135.195.42.168:6007');
echo "<pre>$output</pre>";
$filePath=$\u服务器['DOCUMENT\u ROOT']。'package/myzip'.$owner\u id..zip';
$name=basename($filePath);
$content=“file=@{$name}”;
$output=shell_exec('curl-XPOST-F$contenthttp://135.195.42.168:6007');
回显“$output”;
不幸的是,上面的代码不起作用。有更好的建议吗

$x = 'HELLO';


echo "what you say when you are nice? $x"; //outputs: what you say when you are nice? HELLO
注:

php中的
'
告诉php不要解释
之间的任何内容。

这不适用于
''

回音'\n'//输出\n

回音“\n”//输出换行符(不是换行符,而是回车符)

一般来说,我觉得尽可能使用“”更安全。它不仅更安全,而且通常更实用,因为你可以这样做


您不必重新阅读代码,扫描所有字符串以查找隐藏的$

单引号将打印出
$content
,而不是变量的内容。把它改成双引号,效果很好!非常感谢,@aynber.allow,$output=shell_exec(“curl-XPOST-F$content”);工作正常。
$output = shell_exec("curl -XPOST -F $content 135.195.42.168:6007"); 
$x = 'HELLO';


echo "what you say when you are nice? $x"; //outputs: what you say when you are nice? HELLO
$output = shell_exec('curl -XPOST -F ' . str_replace('a', 'b', $content) . ' http://135.195.42.168:6007');