1

This function is working and move folders from mainfolder to one folder called destinationfolder1. I want to move the folders to one other folder also. Folder called backup, what is in the same place/level as destinationfolder1. Is this possible?

ssh2_sftp_rename($sftp, 'mainfolder/' . $entry  , 'destinationfolder1/' . $entry );
Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
Slistryko
  • 61
  • 5
  • you manage to move or copy? because you can move/rename to one destination only. but you can copy to multiple destinations with multiple calls to this function (not in one go, if there are multiple destinations) – Tamar Jun 16 '17 at 07:15
  • im moving from the mailfolder, but needs it to put folders in 2 directories. – Slistryko Jun 16 '17 at 07:23
  • when you move it is not there any more. what you need is one copy and one move, or two copies and one deletion – Tamar Jun 16 '17 at 07:24
  • The backup folder is containin a lot of folders. The destinationfolder1 folders are changing al the time. – Slistryko Jun 16 '17 at 07:25
  • best way is definitive to copies and one deletion. Can this be done in one prossess? – Slistryko Jun 16 '17 at 07:37

1 Answers1

0

You cannot "rename" a file/folder to two folders. That's nonsense. You have to create a copy of the file/folder.

There's no "copy" function in a core SFTP protocol. There's copy-file SFTP extension for this. But it's not supported by PHP SSH2 functions. You might be able to add the extension to open source phpseclib library. But actually very few SFTP servers do support the extension. In the most widespread OpenSSH SFTP server it is supported only by very recent version 9.0.

If you have a shell access to the server, as a workaround, you can just execute cp shell command using ssh2_exec:

ssh2_exec($connection, 'cp -r /source/path/file /backup/file');

If you do not have a shell access, your only option is to download the file/folder and re-upload it to the other folder.


See also In an SFTP session is it possible to copy one remote file to another location on same remote SFTP server?

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846