Scodigo, Inc.

Log In | Register

Cart Items:
0

Ftp upload contents of a folder

I am new to php, but have managed to cope with uploading specific files by modifying the example script.

I would also like to be able to upload the contents of a specific folder via ftp.

Any help on how to do this with php appreciated.

Example for FTP upload of all files in a folder

Micah Woods's picture

Here's some code that may help get you going. I've done a little testing and it seems to be working fine.

// FTP upload example that uploads all files in a folder

// specify the upload folder (create a folder named Upload on your desktop for this to work or change as desired)
$upload_folder = fm_evaluate('DESKTOP_PA
TH') . '/Upload/';

// make sure we have a directory and that it can be written to
if (! is_dir($upload_folder) or (! is_writable($upload_folder))) {
die("Upload folder does not exist or permissions are invalid.");
}

// set ftp vars
$ftp_server = 'sandbox.scodigo.com';
$ftp_user_name = 'smartpill@sandbox.scodigo.com';
$ftp_user_pass = 'test';
$server_file = 'ftp_upload_test.xml';

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

// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server for user $ftp_user_name\n";
}

// turn passive mode on
ftp_pasv($conn_id, true);

// upload each file
$d = dir($upload_folder);
while (false !== ($entry = $d->read())) {
$file_path = $upload_folder . $entry;
if(is_file($file_path) && substr($entry, 0, 1) != '.') {
// upload the file
$upload = ftp_put($conn_id, $entry, $file_path, FTP_BINARY);
// check upload status
if (!$upload) {
echo "Upload failed for $entry\n";
} else {
echo "Uploaded $file_path to $ftp_server as $entry\n";
}
}
}
$d->close();

// close the ftp stream
ftp_close($conn_id);

Regards,
Micah