【问题标题】:PHP string concat with slashes and variables带有斜杠和变量的 PHP 字符串连接
【发布时间】:2022-01-10 15:15:33
【问题描述】:

我正在尝试通过 PHP 脚本执行 rclone 命令。调用的纯文本版本如下所示:

rclone copy /media/storage/Events/01//999/001 events:testing-events-lowres/Events/01/999/001 --size-only --exclude /HiRes/* --include /Thumbs/* --include /Preview/* -P --checkers 64 --transfers 8 --config /home/admin/.config/rclone/rclone.conf -v --log-file=/www/html/admin/scripts/upload_status/001.json --use-json-log

但是当我运行它时,我错过了一堆东西并得到除以 0 的错误。

exec("rclone copy $baseDir/".$mainEventCode/".$eventCode".  " events:testing-gfevents-lowres/Events/01/$mainEventCode/".$eventCode/" --size-only --exclude /HiRes/* --include /Thumbs/* --include /Preview/* -P --checkers 64 --transfers 8 --config /www/html/admin/scrips/rclone.conf -v --log-file=$directoryName/$eventCode.json --use-json-log");

我尝试了许多 / 和 " 的其他组合,但无法完全拨入。

【问题讨论】:

    标签: php concatenation


    【解决方案1】:

    如果下面的工作正常

    rclone 复制 /media/storage/Events/01//999/001 事件:testing-events-lowres/Events/01/999/001 --size-only --exclude /HiRes/* --include /Thumbs /* --include /Preview/* -P --checkers 64 --transfers 8 --config /home/admin/.config/rclone/rclone.conf -v --log-file=/www/html/admin/ scripts/upload_status/001.json --use-json-log

    下面是相关的 PHP 代码,假设您的变量将包含正确的值。您在连接时犯了一些错误,而不是使用正确的 . (点)和“(引号)。

    exec("rclone 复制 ".$baseDir."/".$mainEventCode."/".$eventCode." events:testing-events-lowres/Events/01/".$mainEventCode."/".$ eventCode。” --size-only --exclude /HiRes/* --include /Thumbs/* --include /Preview/* -P --checkers 64 --transfers 8 --config /www/html/admin/scrips /rclone.conf -v --log-file=$directoryName/".$eventCode.".json --use-json-log");

    【讨论】:

    • 就是这样。谢谢。
    【解决方案2】:

    您可以通过两种不同的方式将变量插入 PHP 中的字符串,并且您可以混合使用这两种方式 - 这可以正常工作,但会使代码更难阅读,正如您所发现的那样。

    第一种方法称为插值,您将整个字符串放在“双引号”内,并且在您提到它们的地方插入任何变量。您不需要使用" 关闭字符串,或者在部分之间使用.。仅使用插值,您的命令可能如下所示:

    exec("rclone copy $baseDir/$mainEventCode/$eventCode events:testing-gfevents-lowres/Events/01/$mainEventCode/$eventCode/ --size-only --exclude /HiRes/* --include /Thumbs/* --include /Preview/* -P --checkers 64 --transfers 8 --config /www/html/admin/scrips/rclone.conf -v --log-file=$directoryName/$eventCode.json --use-json-log");
    

    另一个称为连接,您可以在其中创建多个单独的字符串,然后使用. 运算符将它们连接在一起。某些部分可以是字符串文字,使用“双引号”或“单引号”,它们会成为字符串的一部分就像你写的一样,包括空格、斜线和点。其他部分可以是值为字符串的变量。对于长字符串,这很方便,因为您可以在 . 运算符周围添加换行符而不影响输出。使用串联分解命令可能如下所示:

    exec(
       "rclone copy " . $baseDir . "/" . $mainEventCode . "/" . $eventCode
       . "events:testing-gfevents-lowres/Events/01/" . $mainEventCode
       . "/" . $eventCode . "/ --size-only --exclude /HiRes/*"
       . " --include /Thumbs/* --include /Preview/* -P --checkers 64"
       . " --transfers 8 --config /www/html/admin/scrips/rclone.conf"
       . " -v --log-file=" . $directoryName . "/" . $eventCode . ".json"
       . " --use-json-log"
    );
    

    顺便说一句,在动态生成这样的命令时要非常小心,不要让用户直接控制任何变量,否则他们可以选择运行什么并可能控制您的服务器。

    【讨论】:

      猜你喜欢
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多