IT序号网

php之PHP SSH Lib,回显到文件

qlqwjy 2026年10月07日 编程语言 50 0

我正在尝试创建一个PHP脚本,该脚本通过ssh在远程linux服务器上创建一个文件,并将文件内容回显到其中。

但是,我无法弄清楚如何正确,安全地编码/转义文件内容,以使内容不会被解释为命令。

我正在使用here的phpseclib。

我已经尝试过类似

echo $ssh->exec('sudo echo "' . escapeshellarg($newConfig) . '" > /etc/nginx/nginx.conf') . "\n"; 

但没有成功。

谢谢,

史蒂夫

请您参考如下方法:

那escapeshellcmd呢?引用该vs escapeshellarg的描述:

escapeshellarg() adds single quotes around a string and quotes/escapes any existing single quotes allowing you to pass a string directly to a shell function and having it be treated as a single safe argument.



...和...

escapeshellcmd() escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands. This function should be used to make sure that any data coming from user input is escaped before this data is passed to the exec() or system() functions, or to the backtick operator.

Following characters are preceded by a backslash: #&;`|*?~<>^()[]{}$\, \x0A and \xFF. ' and " are escaped only if they are not paired. In Windows, all these characters plus % are replaced by a space instead.


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!