Page 1 of 1

Having trouble with ftp_get

Posted: Thu May 08, 2008 10:59 am
by thinkadvertising
Hello. I'm having some trouble with php_get() and I don't know what the problem is. I am trying to download (to my web server) all the contents of a remote ftp server directory. I can log into the server just fine, and even display all of the file names I want to download. But as soon as I comment out the print line and add my ftp_get line, I get errors for each file.

The docs on php.net are fairly simple. Rightly so, in my opinion, since the use of this function seems pretty straight forward. I'm clearly doing something wrong. Here is my code:

Code: Select all

 
<?php
$conn = ftp_connect("idx.fnismls.com") or die("Could not connect");
ftp_login($conn,"xxxxxxx","xxxxxxxx"); // connection, user, pass
 
$files = array();
 
$files = ftp_nlist($conn,"idx");
 
// get todays day, format: YYYYMMDD
$timestamp = time() - (24 * 60 * 60); // for the day before
$yesterday = date('Ymd', $timestamp);
 
//print $yesterday . '<br /><br />';
 
for($x = 0; $x < count($files); $x++)
{
    $file = $files[$x];
    $local = $file;
    
    if($file != "idx/." && $file != "idx/..")
    {
        if(strstr($file, $yesterday))
        {
            //print $file . '<br />';
            ftp_get($conn, $local, $file, FTP_BINARY);
        }
        elseif(strstr($file, '.gz'))
        {
            //print $file . '<br />';
            ftp_get($conn, $local, $file, FTP_BINARY);
        }
    }
}
 
 
ftp_close($conn);
?>
 
This gives me nothing but a page full of errors. For example:

Warning: ftp_get(idx/users.txt.gz) [function.ftp-get]: failed to open stream: No such file or directory in /home/lassonet/public_html/idx/get_data.php on line 30

Warning: ftp_get() [function.ftp-get]: Error opening idx/users.txt.gz in /home/lassonet/public_html/idx/get_data.php on line 30

I get that for every file I am trying to download. So...my question is, what am I doing wrong? If I can display the names of the exact files I want, why can't I get them?

I'm lost. I don't know if it's a permissions issue, a coding error, or what. All I want to do is log into a remote ftp server, and then download to my web server all of the contents of a certain directory. Sounds simple, but I'm having some issues with it.

Any help would be great.

Thanks,
Caleb

Re: Having trouble with ftp_get

Posted: Thu May 08, 2008 2:02 pm
by thinkadvertising
Got it. Here's the code that works. I wasn't doing things quite right. No surprise there...

Code: Select all

 
<?php
$conn = ftp_connect("idx.fnismls.com") or die("Could not connect");
ftp_login($conn,"xxxxxx","xxxxxxx"); // connection, user, pass
 
ftp_chdir($conn, "idx");
//ftp_pasv($conn, TRUE);
$files = array();
 
$files = ftp_nlist($conn,".");
 
// get todays day, format: YYYYMMDD
$timestamp = time() - (24 * 60 * 60); // for the day before
$yesterday = date('Ymd', $timestamp);
 
//print $yesterday . '<br /><br />';
//print ftp_pwd($conn) . '<br />';
 
for($x = 0; $x < count($files); $x++)
{
    $file = $files[$x];
    $local = $file;
    
    //print $x . '<br />';
    
    if($file != "idx/." && $file != "idx/..")
    {
        if(strstr($file, $yesterday))
        {
            //print $file . '<br />';
            if(ftp_get($conn, $local, $file, FTP_BINARY))
            {
                print "Files downloaded: " . $file . "<br />";
            }
            else
            {
                print "There has been an error downloading the Image files.<br />";
            }
        }
        elseif(strstr($file, '.gz'))
        {
            //print $file . '<br />';
            if(ftp_get($conn, $local, $file, FTP_BINARY))
            {
                print "Files downloaded: " . $file . "<br />";
            }
            else
            {
                print "There has been an error downloading the database files.<br />";
            }
        }
    }
}
 
 
ftp_close($conn);
?>
 
Enjoy.