0

I'm trying to download the most recent file with the extension add.zip via ftp

I'll preface this by saying I am not well versed in php and have created this frankenstein piece of code from a scattering of other codes.

here is my code

<?php

// set up basic ssl connection
$ftp = ftp_ssl_connect('myremotesite.com');

// login with username and password
$login_result = ftp_login($ftp, 'username', 'password');

if (!$login_result) {
    // PHP will already have raised an E_WARNING level message in this case
    die("can't login");
}

// get list of files on given path
$files = ftp_nlist($ftp, '/1668161919_DATA/*add.zip') or die("Could not find file");

$mostRecent = array(
    'time' => 0,
    'file' => null
);

foreach ($files as $file) {
    // get the last modified time for the file
    $time = ftp_mdtm($ftp, $file);
    

    if ($time > $mostRecent['time']) {
        // this file is the most recent so far
        $mostRecent['time'] = $time;
        $mostRecent['file'] = $file;
    }
}


ftp_get($ftp, "/home/mysite/public_html/wp-content/uploads/data-zipped/target.zip", $mostRecent['file'], FTP_BINARY);
ftp_delete($ftp, $mostRecent['file']);
ftp_close($ftp);
?>

After some testing

<?php

// set up basic ssl connection
$ftp = ftp_ssl_connect('myremotesite.com');

// login with username and password
$login_result = ftp_login($ftp, 'username', 'password');

if ($login_result === TRUE) {
  echo 'woot!';
} else {
  echo 'doh!';
}

Returns a woot

but

// set up basic ssl connection
$ftp = ftp_ssl_connect('myremotesite.com');

// login with username and password
$login_result = ftp_login($ftp, 'username', 'password');

// get list of files on given path
$files = ftp_nlist($ftp, '/1668161919_DATA/*add.zip');

if ($files === TRUE) {
  echo 'files found!';
} else {
  echo 'doh! files not found';
}

?>

Cause a 504 error with no further information.

What am I doing wrong?

When I copy the URL to clipboard in FileZilla the data is sftp://[email protected]/1668161919_DATA

Am I even close to getting this right?

When I have tried using similar FTP file downloads in the past, it was with

ftp_connect

This is the first time I have been forced to use

ftp_ssl_connect


Ok, so I set up a testing environment and ran the script using ftp_connect instead of ftp_ssl_connect and the code worked fine.

I think I need to define my port as 22. But

$ftp = ftp_ssl_connect('myremotesite.com','22')

doesn't work. Can anyone assist?

7
  • What's your question/problem? + Btw, this is definitely not "Secure" FTP. Commented Dec 6, 2022 at 6:36
  • The file doesn't download. when I run the script it just times out.
    – PaulW32
    Commented Dec 6, 2022 at 6:40
  • Where does your question say that? Where does it the code hang exactly? What did you do to debug the problem? Commented Dec 6, 2022 at 6:44
  • The logins work, but something about the file download is not working. If I change the username or password, I get the 'can't login' error, so the issue is at some point past this. The URL given to access the files is not FTP, it is https trying to access via FTP doesn't work. I have previously used a simple // new connect $conn = ftp_connect('ftp.mysite.com'); ftp_login($conn, 'username', 'password'); But it doesn't work for this site.
    – PaulW32
    Commented Dec 6, 2022 at 6:49
  • You didn't answer my questions. Where does it hang? There are 30 lines after the ftp_login. On which does it hang? Does ftp_nlist work? + Never post any information in comments. Always edit everything you know/you find into your question. Commented Dec 6, 2022 at 7:08

0

Browse other questions tagged or ask your own question.