Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a folder through with FTP using PHP

Tags:

directory

php

ftp

How can i donwload a folder from a remote host using FTP And PHP?

I have username and password and the folder to donwload...

copy()?

Let me know, thanks!

like image 651
Damiano Avatar asked Apr 13 '11 13:04

Damiano


1 Answers

<?php 
$ftp_server = "ftp.example.com"; 
$conn_id = ftp_connect ($ftp_server) 
    or die("Couldn't connect to $ftp_server"); 
    
$login_result = ftp_login($conn_id, "user", "pass"); 
if ((!$conn_id) || (!$login_result)) 
    die("FTP Connection Failed"); 

ftp_sync ("DirectoryToCopy");    // Use "." if you are in the current directory 

ftp_close($conn_id);  

// ftp_sync - Copy directory and file structure 
function ftp_sync ($dir) { 

    global $conn_id; 

    if ($dir != ".") { 
        if (ftp_chdir($conn_id, $dir) == false) { 
            echo ("Change Dir Failed: $dir<BR>\r\n"); 
            return; 
        } 
        if (!(is_dir($dir))) 
            mkdir($dir); 
        chdir ($dir); 
    } 

    $contents = ftp_nlist($conn_id, "."); 
    foreach ($contents as $file) { 
    
        if ($file == '.' || $file == '..') 
            continue; 
        
        if (@ftp_chdir($conn_id, $file)) { 
            ftp_chdir ($conn_id, ".."); 
            ftp_sync ($file); 
        } 
        else 
            ftp_get($conn_id, $file, $file, FTP_BINARY); 
    } 
        
    ftp_chdir ($conn_id, ".."); 
    chdir (".."); 

} 
?>

Source: https://www.php.net/manual/function.ftp-get.php#90910

like image 74
hectorct Avatar answered Sep 22 '22 06:09

hectorct