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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.