hi,
i need copy a local folders and files to a ftp server
for example i call this:

copy ( $ftpserver, $user, $password , local path + file );

or copy ( $ftpserver, $user, $password , local path / folders / folder + file );

the script must see all sub dirs and creat the same struture for copy the file
that i need

any one know a script that can do that?
i have found this:

but for some reason this don´t work for me.

thanks for your help
:)

Dani AI

Generated

A compact, practical path: as suggested, a framework helper that implements mirroring is the quickest route; as noted, the native PHP ftp_* functions are fully capable when a framework isn't wanted. The basic strategy is simple and reliable: walk the local tree top‑down, create each remote directory before files inside it, then upload files (binary for non-text). The weberdev example referenced in the thread can fail for environment reasons; the checklist below helps isolate those causes.

Key checklist (common failure points)

  • Verify connection and credentials first (simple ftp_connect/ftp_login test).
  • Enable passive mode (ftp_pasv) when behind NAT/firewalls.
  • Normalize and trim local and remote paths; avoid double slashes and trailing slashes.
  • Iterate directories top‑down (create dirs before uploading files) so parent folders exist.
  • Use FTP_BINARY for images/binaries; use FTP_ASCII only for plain-text when necessary.
  • Check return values of ftp_mkdir and ftp_put and log errors; many failures are permission or path related (550).
  • Watch PHP limits (max_execution_time, memory); for very large transfers use CLI scripts or split uploads.
  • Symlinks, special files, and server path rules differ between hosts; test with a small sample tree first.

Minimal, practical example (pattern to adapt):

// Minimal mirror: create dirs then upload files (top-down)
function ftp_mirror($ftp, $localRoot, $remoteRoot) {
    $localRoot  = rtrim($localRoot, "/\\");
    $remoteRoot = rtrim($remoteRoot, "/");

    $it = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($localRoot, RecursiveDirectoryIterator::SKIP_DOTS),
        RecursiveIteratorIterator::SELF_FIRST
    );

    foreach ($it as $item) {
        $localPath    = $item->getPathname();
        $relativePath = substr($localPath, strlen($localRoot) + 1);
        $remotePath   = $remoteRoot . '/' . str_replace('\\', '/', $relativePath);

        if ($item->isDir()) {
            @ftp_mkdir($ftp, $remotePath); // ignore "already exists"
        } else {
            if (!@ftp_put($ftp, $remotePath, $localPath, FTP_BINARY)) {
                error_log("FTP upload failed: $localPath -> $remotePath");
            }
        }
    }
}

// usage:
// $conn = ftp_connect('host'); ftp_login($conn,'user','pass'); ftp_pasv($conn,true);
// ftp_mirror($conn,'/local/path','/remote/path'); ftp_close($conn);

Notes: FTP does not reliably preserve timestamps; servers differ in accepted path formats (absolute vs. login‑relative). For secure, resumable, or high‑speed syncs prefer SFTP/rsync when available. If the weberdev script failed in this thread, start by testing a single file upload and enabling passive mode to reveal the underlying error.

Recommended Answers

All 2 Replies

Try CodeIgniter's FTP Class -
It offers a mirroring capability so that you can recreate a remote directory tree perfectly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.