简体   繁体   中英

Escaping spaces in WinSCP command-line when executing from PHP

I have a what should be a simple problem. I am trying to execute the code below in PHP. The goal of this code is to upload files from one folder to another folder in an FTP using a program called WinSCP.

exec("C:\\xampp\\winscp.com backup /command \"option confirm off\" \"put C:\\big boy\\Documents\\dev notes\\\" \"exit\"");

The files do not get transferred and the problem I think I am having is because of the spaces I have in my directory where big boy and dev notes are. I tried including another double quote between:

C:\\big boy\\Documents\\dev notes\\\

but it did not work.

Let's first off all bring this down to earth a little bit. The following line:

exec("C:\\xampp\\upload.exe backup /command \"option confirm off\" \"put C:\\big boy\\Documents\\dev notes\\\" \"exit\"");

You're mainly interested in the actual string of the command, let's rewiew it:

C:\xampp\upload.exe backup /command "option confirm off" ⤦ 
⤥"put C:\big boy\Documents\dev notes\" "exit"

As it should now be obviously visible to you, this is not a valid command in shell. Consult the documentation of upload.exe in which format the value of the /command switch has to be passed.

As you have not provided any reference what upload.exe is, I can not give a more concrete suggestion here. However, one common way to deal with these problems is to first assign the command to a variable and then execute it. This allows to display debugging information which then allows to easily fix things:

$command = "C:\\xampp\\upload.exe backup /command \"option confirm off\" \"put C:\\big boy\\Documents\\dev notes\\\" \"exit\"";
exec($command);

It might be that it's just (guessed only, this is how it work with cmd /k ):

$command = 'C:\xampp\upload.exe backup /command ""option confirm off" ⤦ 
⤥"put "c:\big boy\Documents\dev notes\" "exit""';

Edit: And now as you wrote it is actually the winscp.com command, I would assume:

$command = 'C:\xampp\upload.exe backup /command "option confirm off" ⤦ 
⤥"put ""c:\big boy\Documents\dev notes\""" "exit"';

With the following rules:

  • Each single command has to be wrapped into " quotes if it contains spaces.

     option confirm off "option confirm off" 
  • If a command contains also " quotes, those have to be doubled ""

     put "c:\\big boy\\Documents\\dev notes\\" "put ""c:\\big boy\\Documents\\dev notes\\""" 

像这样用单引号引起来:

exec("cd 'C:\\Program Files'");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM