Jump to content

[SOLVED] Download all files from directory


EagleAmerican

Recommended Posts

I'm trying to make a script to download all files from an ftp directory to my hosting. I know I need to use a loop but not sure where to go from here.

 

Any help appreciated. :)

 

<?php
$ftp_server = "";
$uname = ""; //Enter your ftp username here.
$pwd = ""; //Enter your ftp password here.
$directory = "" //Enter the dir of the files on the server here.

$connection = ftp_connect($ftp_server) or die("Error connecting to $ftp_server");
$login = ftp_login($connection, $uname, $pwd);
if (ftp_chdir($connection, $directory)) {
    echo "Changed directory to: " . ftp_pwd($connection) . "\n";
} else { 
    echo "Error while changing directory!\n";
}

ftp_pasv($connection,TRUE);

$count = count(ftp_nlist($connection, "."));



ftp_close($connection);
?>

I think this would work:

<?php
$ftp_server = "";
$uname = ""; //Enter your ftp username here.
$pwd = ""; //Enter your ftp password here.
$directory = "" //Enter the dir of the files on the server here.

$connection = ftp_connect($ftp_server) or die("Error connecting to $ftp_server");
$login = ftp_login($connection, $uname, $pwd);
if (ftp_chdir($connection, $directory)) {
    echo "Changed directory to: " . ftp_pwd($connection) . "\n";
} else { 
    echo "Error while changing directory!\n";
}

ftp_pasv($connection,TRUE);

$files = ftp_nlist($connection, ".");
foreach ($files as $file)
{
   $newFile = fopen($file, 'w');
   ftp_nb_fget($connection, $newFile, $file, FTP_BINARY);
   fclose($newFile);
}



ftp_close($connection);
?>

Progress:

 

I got the script working by doing php ftp.php in the shell and it downloaded all the files, However most of the files were 2.9K and the rest 1.4K yet on the FTP server they were over 5mb for most. Any ideas how to get it to download the whole file?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.