gpt4 book ai didi

php - 在 PHP CLI 脚本参数中传递路径

转载 作者:行者123 更新时间:2023-12-05 08:00:31 24 4
gpt4 key购买 nike

我正在编写一个 php CLI 脚本,它接受路径以及其他参数。

举个例子:

php myscript.php -p=/Volumes/Macintosh HD/Users/andrea/samples

脚本有自己的方式来红色参数,它正确地获取了 -p 的值,将它设置在一个名为 $project_path 的变量中。

但是,当我使用 isdir($project_path) 测试文件夹时,它返回 false。

我尝试以不同的方式传递路径:

  • /Volumes/Macintosh\HD/Users/andrea/samples
  • '/Volumes/Macintosh HD/Users/andrea/samples'
  • “/Volumes/Macintosh HD/Users/andrea/samples”
  • '/Volumes/Macintosh\HD/Users/andrea/samples'
  • “/Volumes/Macintosh\HD/Users/andrea/samples”

它们都不起作用。我必须使用什么格式才能使其正常工作?请考虑该脚本还必须适用于不同的操作系统(即 Windows)。

The problem is the path argument is automatically escaped: I need to unescape it. The returned string is:

\'/Volumes/Macintosh\ HD/Users/andrea/samples\'

最佳答案

简答:使用 escapeshellarg()

长答案:

chmod +x yourscript.php

-

$path = '/Volumes/Macintosh HD/Users/andrea/samples';
$cmdline = sprintf('/home/user/yourscript.php -p=%s 2>&1', escapeshellarg($path));
$output = shell_exec($cmdline);

示例 cli 脚本:

#!/usr/bin/php
<?php

fwrite(STDOUT, print_r($_SERVER, TRUE));

exit(0); // exit with exit code 0

?>

关于php - 在 PHP CLI 脚本参数中传递路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18217988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com