tags:

views:

276

answers:

1

Very simple premise. PHP as a shell script, log into an ftp (any ftp, i tried 2), get into a remote directory, list files, download them to local one. it's not working and it's not producing any meaningful errors. it logs in, it gets a list of files, tries the ftp_get and returns false.

#!/usr/bin/php -q
<?PHP

chdir("/www/cron/");

$server     = "somedomain.com";
$user       = "username";
$pass       = "password";
$localDir   = "./ratings"; // directory exists and is writeable by 777
$remoteDir  = "httpdocs/js"; // any will do.

chdir($localDir);

$conn_id        = @ftp_connect($server);
$login_result   = ftp_login($conn_id, $user, $pass);

if ($login_result) {
    echo "Logged into ftp://$user:$pass@$server FTP...\n";
    if (!ftp_chdir($conn_id, $remoteDir))
        echo "Could not cwd to $remoteDir\n";

    $current = ftp_pwd($conn_id); // reports change to ratings
    $buff = ftp_nlist($conn_id, "."); // gets listings
    array_shift($buff); // get rid of . and ..
    array_shift($buff);
    // ftp_pasv($conn_id, true); // makes no difference
    if (count($buff))
    foreach($buff as $file) {
        echo "getting [$current/$file] as [" . getcwd() . "/$file]... ";
        if (!ftp_get($conn_id, $file, $file, FTP_ASCII, 0))
            echo "failed.\n";
        else
            echo "done.\n";
    }

    ftp_close($conn_id);
}
else {
    echo "Could not login to ftp!";
}
?>

here is the output when pointed to my js folder:

Logged into ftp://username:[email protected] FTP...
getting [/httpdocs/js/mootools-1.2-core.js] as [/www/cron/ratings/mootools-1.2-core.js]... failed.
getting [/httpdocs/js/moo_rating.js] as [/www/cron/ratings/moo_rating.js]... failed.
getting [/httpdocs/js/moo_form.js] as [/www/cron/ratings/moo_form.js]... failed.
getting [/httpdocs/js/dci_core-1.3.js] as [/www/cron/ratings/dci_core-1.3.js]... failed.
getting [/httpdocs/js/moo_browser.user.js] as [/www/cron/ratings/moo_browser.user.js]... failed.
getting [/httpdocs/js/Lasso.Crop.js] as [/www/cron/ratings/Lasso.Crop.js]... failed.
getting [/httpdocs/js/Lasso.js] as [/www/cron/ratings/Lasso.js]... failed.
getting [/httpdocs/js/lighter] as [/www/cron/ratings/lighter]... failed.
getting [/httpdocs/js/mootools-1.2.1-core.js] as [/www/cron/ratings/mootools-1.2.1-core.js]... failed.
getting [/httpdocs/js/Lighter.js] as [/www/cron/ratings/Lighter.js]... failed.
getting [/httpdocs/js/Fuel.ruby.js] as [/www/cron/ratings/Fuel.ruby.js]... failed.
getting [/httpdocs/js/Fuel.php.js] as [/www/cron/ratings/Fuel.php.js]... failed.
getting [/httpdocs/js/Fuel.js.js] as [/www/cron/ratings/Fuel.js.js]... failed.
getting [/httpdocs/js/Fuel.html.js] as [/www/cron/ratings/Fuel.html.js]... failed.
getting [/httpdocs/js/Fuel.css.js] as [/www/cron/ratings/Fuel.css.js]... failed.
getting [/httpdocs/js/Flame.twilight.js] as [/www/cron/ratings/Flame.twilight.js]... failed.
getting [/httpdocs/js/Flame.panic.js] as [/www/cron/ratings/Flame.panic.js]... failed.
getting [/httpdocs/js/Flame.mocha.js] as [/www/cron/ratings/Flame.mocha.js]... failed.
getting [/httpdocs/js/Flame.git.js] as [/www/cron/ratings/Flame.git.js]... failed.
getting [/httpdocs/js/fragged.js] as [/www/cron/ratings/fragged.js]... failed.
getting [/httpdocs/js/ImageScrOOler.js] as [/www/cron/ratings/ImageScrOOler.js]... failed.
getting [/httpdocs/js/ImageScrOOler2.js] as [/www/cron/ratings/ImageScrOOler2.js]... failed.
getting [/httpdocs/js/mootools-1.2.3-core-yc.js] as [/www/cron/ratings/mootools-1.2.3-core-yc.js]... failed.
getting [/httpdocs/js/mootools-1.2.3.1-more.js] as [/www/cron/ratings/mootools-1.2.3.1-more.js]... failed.
getting [/httpdocs/js/hOOmanTest.js] as [/www/cron/ratings/hOOmanTest.js]... failed.
getting [/httpdocs/js/mootools-1.2.3-core-nc.js] as [/www/cron/ratings/mootools-1.2.3-core-nc.js]... failed.
getting [/httpdocs/js/mootools_geoData.js] as [/www/cron/ratings/mootools_geoData.js]... failed.
getting [/httpdocs/js/largerBox.js] as [/www/cron/ratings/largerBox.js]... failed.

short on running external ftp or wget to fetch files, what are my options here?

i have tested ftp_put and that works fine. the script runs as root so it won't be a local permissions issue. thanks in advance...

A: 

I would try passive mode by calling this after ftp_login:

ftp_pasv($conn_id, TRUE);
Petr Peller