...

View Full Version : Php Ftp



ccurle
03-14-2007, 09:16 PM
I am trying to transfer a folder with files from server to another.

I found a script online and it sort of works. What it does is copy files from the root folder (which it shouldn't) and puts into the folder I am trying to copy from.

What I need is it to copy from the $ftproot into the $srcroot/$srcela folder.

Can someone please tell me what I am doing wrong.


// --------------------------------------------------------------------
// THE TRIGGER
// --------------------------------------------------------------------

// set the various variables
$ftproot = "/mnt/channel/Upload/16_2/";
$srcroot = "/var/www/html/slides/";
$srcrela = "16_2/";

// connect to the destination FTP & enter appropriate directories both locally and remotely
$ftpc = ftp_connect("localhost");
$ftpr = ftp_login($ftpc,"username","password");

if ((!$ftpc) || (!$ftpr)) { echo "FTP connection not established!"; die(); }
if (!chdir($srcroot)) { echo "Could not enter local source root directory."; die(); }
if (!ftp_chdir($ftpc,$ftproot)) { echo "Could not enter FTP root directory."; die(); }

// start ftp'ing over the directory recursively
ftpRec ($srcrela);

// close the FTP connection
ftp_close($ftpc);

// --------------------------------------------------------------------
// THE ACTUAL FUNCTION
// --------------------------------------------------------------------
function ftpRec ($srcrela) {
global $srcroot;
global $ftproot;
global $ftpc;
global $ftpr;

// enter the local directory to be recursed through
chdir($srcroot.$srcrela);

// check if the directory exists & change to it on the destination
if (!ftp_chdir($ftpc,$ftproot.$srcrela)) {
// remote directory doesn't exist so create & enter it
ftp_mkdir ($ftpc,$ftproot.$srcrela);
ftp_chdir ($ftpc,$ftproot.$srcrela);
}

if ($handle = opendir(".")) {
while (false !== ($fil = readdir($handle))) {
if ($fil != "." && $fil != "..") {
// check if it's a file or directory
if (!is_dir($fil)) {
// it's a file so upload it
ftp_put($ftpc, $ftproot.$srcrela.$fil, $fil, FTP_BINARY);
} else {
// it's a directory so recurse through it
if ($fil == "templates") {
// I want the script to ignore any directories named "templates"
// and therefore, not recurse through them and upload their contents
} else {
ftpRec ($srcrela.$fil."/");
chdir ("../");
}
}
}
}
closedir($handle);
}
}

timgolding
03-14-2007, 10:48 PM
Ive got no idea but If I was doing it I would probably use simple file reading and writing. Its no fuss :)

ccurle
03-14-2007, 11:23 PM
Ive got no idea but If I was doing it I would probably use simple file reading and writing. Its no fuss :)

With what I am doing, it is a fuss.

Fumigator
03-15-2007, 12:20 AM
Looks like you've just got the remote and local file paths backwards on the ftp_put() (http://us2.php.net/ftp_put) function.

firepages
03-15-2007, 04:30 AM
whilst I am one of the biggest culprits for trying to use PHP for everything ... surely this would be a better job for rsync (http://samba.anu.edu.au/rsync/) or unison (http://www.cis.upenn.edu/~bcpierce/unison/) or similar ?

ccurle
03-15-2007, 10:25 PM
I can't use those. I am trying to build an app that moves files when needed.

I need to find a way of copying a directory from one server to another.

The above sort of works, but copies the wrong things.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum