Downloading a Folder Through with Ftp Using PHP

Downloading a file via ftp using php

Use ftp_get not ftp_put

<?php

// define some variables
$local_file = 'local.zip';
$server_file = 'server.zip';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}

// close the connection
ftp_close($conn_id);

?>

ftp_get from PHP manual

PHP FTP - Download files in certain directory instead of root

You can do this very easily by using this library :

The code :

$connection = new FtpConnection('host', 'username', 'password');

$client = new FtpClient($connection);

if (asyncDownload('Users/John/Site/Code/PDF', '.')) {
echo 'Done!';
}

function syncDownload($localDir, $remoteDir)
{
global $client;

if (!is_dir($localDir)) {
mkdir($localDir);
}

/**
* listDirectoryDetails method will recursively gets all the files with
* their details within the giving directory.
*/
$files = $client->listDirectoryDetails($dir, true);

foreach($files as $file) {
$client->download($file['path'], $localDir . '/' . $file['name'], true, FtpWrapper::BINARY);
}

return true;
}

FTP download folder with php

What i would do, is downloading all the files in the /tmp directory and
then zip them all together into an archive.
This file can then be streamed for as a response.

Make sure though that your tmp directory gets cleared, which normaly php does on its own after the request finished see here.

PHP ZipArchive

If you want to download all files within a single request (not within a folder).

This could work when using the 'Content-Disposition' value as a seperator between the file contents.
I cannot confidently explain you what you would need for that.
Maybe googling for 'mulipart form request' will give you a tutorial on that.

As for creating a direct folder as a response, i have never seen such a feature and don't think it is possible.

In my opinion Zip is the best option here, as it should be supported widely enough,
and keeps your folders intact, wich i think you want to keep.

PHP FTP library/function to download whole directory and sub directories to local drive

http://php.net/manual/en/function.ftp-get.php

Look in the comments: mroerick at gmx dot net 15-May-2009 07:42

Idea is to: login > get list of files > download the files, as the example does.

List and download clicked file from FTP

Your link in the generated <a> tag points back to the web server, which does not contain the linked file.

What you need to do is to link to a PHP script, giving it a name of the file to download. The script will then download the file from an FTP server and pass the downloaded file back to the user (to the webbrowser).

echo "<a href=\"download.php?file=".urlencode($file)."\">".htmlspecialchars($file)."</a>";

A very trivial version of the download.php script:

<?

header('Content-Type: application/octet-stream');

echo file_get_contents('ftp://username:password@ftp.example.com/path/' . $_GET["file"]);

The download.php script uses FTP URL wrappers. If that's not allowed on your web server, you have to go the harder way with FTP functions. See
PHP: How do I read a file from FTP server into a variable?


Though for a really correct solution, you should provide some HTTP headers related to the file, like Content-Length, Content-Type and Content-Disposition.

Also the above trivial example will first download whole file from the FTP server to the webserver. And only then it will start streaming it to the user (webbrowser). What is a waste of time and also of a memory on the webserver.

For a better solution, see Download file via PHP script from FTP server to browser with Content-Length header without storing the file on the web server.

You may also want to autodetect Content-Type, unless all your files are of the same type.


A related question about implementing an FTP upload with a webpage:

Displaying recently uploaded images from remote FTP server in PHP

How does one move files to a specific folder after an FTP download using PHP?

The ftp_get($conn_id, $file, $file, FTP_BINARY); should be able to place your remote file anywhere you want as a default, you just have to indicate that spot in the local parameter:

# Where ever you want to download local files to
$dir = __DIR__.'/my/specific/path/';
# See if directory exists, create if not
if(!is_dir($dir))
mkdir($dir,0755,true);
# Saves the file(s) into the $dir folder with the same name as the remote file
ftp_get($conn_id, $dir.$file, $file, FTP_BINARY);

PHP download entire folder (recursive) via FTP

There are probably better mechanisms to do what you want to do.

First, can you use sftp or scp from one host to the other?

scp -R username@oldhost:path/to/directory/ /path/to/destination/directory

or

sftp username@oldhost  # then use 'get -r' to download recursively

or

rsync -avz -P username@oldhost:/path/to/directory/ /path/to/destination/directory/

The -P makes it easy to restart a stalled/dead download.

If good tools won't work, then see if wget is installed:

wget --mirror --continue --ftp-user=username ftp://oldhost/path/to/directory/

The --continue makes it easier to restart a stalled/dead download.



Related Topics



Leave a reply



Submit