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