Home > Software engineering >  PHP string concat with slashes and variables
PHP string concat with slashes and variables

Time:12-05

I am trying to exec an rclone command via a PHP script. The plain text version of the call looks like this:

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

But when I run it, I'm missing a bunch of stuff and getting divide by 0 errors.

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");

I've tried a number of other combinations of / and " and can't quite get it dialed in.

CodePudding user response:

If the below is working fine

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

Then below is the related PHP code, assuming your variables will contain correct values. You are doing some mistake while concatenating, not using proper . (dots) and " (quotes).

exec("rclone copy ".$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");

CodePudding user response:

There are two different ways you can insert variables into strings in PHP, and you're mixing both - which can work fine, but makes the code harder to read, as you've found.

The first way is called interpolation, where you put the whole string inside "double quotes", and any variables are inserted where you mention them. You don't need to close the string with ", or use . between parts. Using just interpolation, your command could look like this:

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");

The other is called concatenation, where you create multiple separate strings, and join them together with the . operator. Some of the parts can be string literals, using either "double quotes" or 'single quotes', which become part of the string exactly as you write them, including spaces, slashes, and dots. Other parts can be variables whose value is a string. For long strings, this is handy because you can add line breaks around the . operator without affecting the output. Breaking your command down with concatenation might look like this:

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"
);

Incidentally, be very careful when dynamically generating commands like this not to let the user directly control any of the variables, otherwise they can choose what gets run and potentially take control of your server.

  • Related