views:

33

answers:

2

I'm running a php script via command line, and it works just fine, except that when it's finished, it doesn't go back to the command line? Just sits there, so I never when it's done...

This is the script:

             $conn_id = ftp_connect(REMOTE)
             or die("Couldn't connect to ".REMOTE);

            $login_result = ftp_login($conn_id, 'OMITTED','OMITTED');
            if ((!$conn_id) || (!$login_result))
               die("FTP Connection Failed");
               $dir = 'download';
               if ($dir != ".") {

                    if (ftp_chdir($conn_id, $dir) == false) {
                       echo ("Change Dir Failed: $dir<BR>\r\n");
                       return;
                    }
                    if (!(is_dir($dir)))
                       mkdir($dir);
                       chdir ($dir);
                    }

                    $contents = ftp_nlist($conn_id, ".");
                    foreach ($contents as $file) {
                            if ($file == '.' || $file == '..')
                            continue;

                            if (@ftp_chdir($conn_id, $file)) {
                               ftp_chdir ($conn_id, "..");
                               ftp_sync ($file);
                            }
                    else
                            ftp_get($conn_id, $file, $file, FTP_BINARY);
                    }

                    ftp_chdir ($conn_id, "..");
                    chdir ("..");
                    ftp_close($conn_id);
A: 

You're missing some }, and in general some of the code formatting makes it difficult to read. Try this:

<?php
$conn_id = ftp_connect(REMOTE) or die("Couldn't connect to ".REMOTE);

$login_result = ftp_login($conn_id, 'OMITTED','OMITTED');
if ((!$conn_id) || (!$login_result))
    die("FTP Connection Failed");

$dir = 'download';
if ($dir != ".") {

    if (ftp_chdir($conn_id, $dir) == false) {
        echo ("Change Dir Failed: $dir<BR>\r\n");
        return;
    }
    if (!(is_dir($dir))) {
        mkdir($dir);
        chdir ($dir);
    }

    $contents = ftp_nlist($conn_id, ".");
    foreach ($contents as $file) {
        if ($file == '.' || $file == '..')
            continue;

        if (@ftp_chdir($conn_id, $file)) {
            ftp_chdir ($conn_id, "..");
            ftp_sync ($file);
        } else {                  
            ftp_get($conn_id, $file, $file, FTP_BINARY);
        }
    }

    ftp_chdir ($conn_id, "..");
    chdir ("..");
    ftp_close($conn_id);
}

I also highly recommend adopting some form of coding standards. PEAR Coding Standards is a good place to start

ctshryock
And you may or may not need the `<?php` at the beginning, depending on how you're running the script
ctshryock
A: 

If it is not returning back to Command line then It is in some kind of infinite loop then.

Arsheep