Skip to content

Instantly share code, notes, and snippets.

@thinkyhead
Created October 6, 2012 00:35
Show Gist options
  • Save thinkyhead/3843254 to your computer and use it in GitHub Desktop.
Save thinkyhead/3843254 to your computer and use it in GitHub Desktop.
Drupal: FTP the output of a View
<?php
function _my_export_view_via_ftp($nid) {
$out = 'FTP ERROR'; // assume the worst
// example view/display/ftp info/filename
$view_name = 'feeds';
$display_id = 'views_display_name';
$host = 'ftp.mydomain.com'; $user = 'myftpuser'; $pass = 'myftppass';
$filename = "node{$nid}.xml";
// get the output of the view/display
$view = views_get_view($view_name);
$output = $view->preview($display_id, array($nid));
// write out to /tmp
$fp = tmpfile();
fwrite($fp, $output);
// connect to the ftp server
$ftp = ftp_connect($host);
if ($ftp && ftp_login($ftp, $user, $pass)) {
fseek($fp, 0); // remember to rewind!
ftp_pasv($ftp, TRUE);
if (ftp_fput($ftp, $filename, $fp, FTP_BINARY)) {
$out = 'FTP COMPLETE';
}
}
return $out; // e.g., the output for a page callback
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment