I personally run for SFTP or FTPS a custom PHP script in PASE since years that get called by a CL to have control.
In any case it recommended in this scenarios to get a file list, download the file, one by one, to local dir, then delete it in case.
Something like the script below (I don't know if it will get formatted or messed but just to have the gist of it YMMV), it supports public key or password.echo go to stdout so it usually produce a nice spool that can then be used or to screen.db2i_logs is a custom functions that logs to db2
error_reporting(E_ALL|E_PARSE);ini_set('error_reporting', E_ALL);ini_set('display_errors', 1);
$DIRWHITELIST = ['/tmp', '/local'];
$HOSTDEF="demo.er.com";$USERDEF="demo";$SSHPASSDEF="demo";$REMOTEDIRDEF="/upload";$LOCALDIR="/home/aaa";$SSHPORT=2222;$SSHPORTDEF=22;$AUTHDEF = "PWD";$PUBKEYFILEDEF = '/onnkeys/ACKW6_SFTP_AMZN_2.pub';$PRIKEYFILEDEF = '/onnkeys/ACKW6_SFTP_AMZN_20210223T091102038';$PUBKEYFILEDEF = '';$PRIKEYFILEDEF = '';
$HOST = trim(getenv('SFTPHOST')) ?: $HOSTDEF;$USER = trim(getenv('SFTPUSER')) ?: $USERDEF;$SSHPASS = trim(getenv('SFTPPASS')) ?: $SSHPASSDEF;$REMOTEDIR = trim(getenv('SFTPREMOTEDIR')) ?: $REMOTEDIRDEF;$LOCALDIR = trim(getenv('SFTPLOCALDIR')) ?: $LOCALDIR;$SSHPORT = trim(getenv('SFTPPORT')) ?: $SSHPORTDEF;$AUTH = trim(getenv('SFTPAUTH')) ?: $AUTHDEF;$PUBKEYFILE = trim(getenv('SFTPPUBKEY')) ?: $PUBKEYFILEDEF;$PRIKEYFILE = trim(getenv('SFTPPRIKEY')) ?: $PRIKEYFILEDEF;
if (!function_exists("ssh2_connect")) { die("sftpdwndir: function ssh2_connect not found, you cannot use ssh2 here");} if(!array_any($DIRWHITELIST, fn($p) => str_starts_with($LOCALDIR, $p))) { die("sftpdwndir: localdir NOT allowed, aborting\n");}
$con = ssh2_connect($HOST, $SSHPORT);if($con === false) { echo "*** SFTP *** ERROR connecting ".$HOSTDEF."\n"; exit(1);}if($AUTH == "PRIKEY") { $proto = "SFTPDWN_PWD"; $r = ssh2_auth_pubkey_file($con, $USER, $PUBKEYFILE, $PRIKEYFILE); if($r === false) { echo "*** SFTP *** PUBKEY ERROR AUTH $USER $PRIKEYFILE\n"; exit(1); }} else { $proto = "SFTPDWN_PRIKEY"; $r = ssh2_auth_password($con, $USER, $SSHPASS); if($r === false) { echo "*** SFTP *** PWD ERROR authenticating $USER\n"; exit(1); }}
echo "*** SFTP *** Server : $HOST ($AUTH)\n";$sftp = ssh2_sftp($con);
$handle = opendir("ssh2.s
ftp://".intval($sftp).$REMOTEDIR);echo "*** SFTP *** Server directory: ".$REMOTEDIR, "\n";echo "*** SFTP *** Local directory: ".$LOCALDIR, "\n";while ($entry = readdir($handle)){ $fileurl = 'ssh2.sftp://'.intval($sftp).$REMOTEDIR.'/'.$entry; $destpath = $LOCALDIR."/".$entry; if(is_dir($fileurl)) { continue; } $modified = date("Y-m-d H:i:s", filemtime($fileurl)); $size = filesize($fileurl); echo "*** SFTP *** Downloading ".$entry." ".$modified."\n"; $contents = file_get_contents($fileurl); if($contents === false) { echo "*** SFTP *** ERROR downloading ".$entry."\n"; echo "*** SFTP *** STOPPING\n"; db2i_logs($HOST, $REMOTEDIR."/".$entry, "ERROR downloading", $proto); exit(1); } db2i_logs($HOST, $REMOTEDIR."/".$entry, "Size $size bytes, mod $modified", $proto); $returncode = file_put_contents($destpath, $contents); if($returncode === false) { echo "*** SFTP *** ERROR writing ".$entry."\n"; echo "*** SFTP *** STOPPING\n"; db2i_logs($HOST, $REMOTEDIR."/".$entry, "ERROR writing", $proto); exit(1); } unlink($fileurl);}closedir($handle);echo "*** SFTP *** Disconnecting...", "\n";ssh2_disconnect($con);
On Thursday, January 8, 2026 at 10:18:44 PM GMT+1, Greg Wilburn <gwilburn@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
We are doing an increasing number of data exchanges using SFTP... I have been using the OpenSSH and scripting to accomplish this. However, we recently ran into issues where our script may have inadvertently deleted files.
The script runs "get *.csv" followed by "rm *.csv".
We think that the other party was uploading files at the same time I was downloading (several hundred or more files). So I'm looking for some way (or something) to better manage this for me.
TIA,
Greg
[Logo]<
https://www.totalbizfulfillment.com/> Greg Wilburn
Director of IT
301.895.3792 ext. 1231
301.895.3895 direct
gwilburn@xxxxxxxxxxxxxxxxxxxxxxx<mailto:gwilburn@xxxxxxxxxxxxxxxxxxxxxxx>
1 Corporate Dr
Grantsville, MD 21536
www.totalbizfulfillment.com<
http://www.totalbizfulfillment.com>
As an Amazon Associate we earn from qualifying purchases.