Get all the PDF files from FTP server in different folders Well, quite a biggie with all the foreach's. It works, it gets me 981 results, but there must be a way to shorten this code. Right?

&lt;?php

$class = new FTPcleanup();
echo $class-&gt;index();


class FTPcleanup {

    private $server = "1.1.1.1";
    private $user   = "ftpuser";
    private $passwd = "ftppassword";
    private $path   = "/Folder/Productthingfolderthing/";

    private function connect_ftp() {
        $connection = ftp_connect($this-&gt;server);

        ftp_login($connection, $this-&gt;user, $this-&gt;passwd);
        ftp_chdir($connection, $this-&gt;path);

        return $connection;
    }

    private function connect_sql() {

    }

    private function get_files_from_ftp() {
        $FinalObjectList = array();

        $connection = $this-&gt;connect_ftp();

        $folders = ftp_nlist($connection, ".");

        foreach($folders as $folder) {
            ftp_chdir($connection, $this-&gt;path.$folder);

            $subfolders = ftp_nlist($connection, ".");
            if (count($subfolders !== 0)) {
                foreach ($subfolders as $subfolder) {

                    if (ftp_size($connection, $subfolder) == "-1") {
                        //Folder, open and go on to child folders
                        if(ftp_chdir($connection, $this-&gt;path.$folder."/".$subfolder)){
                            $childfolders = ftp_nlist($connection, ".");

                            if (count($childfolders !== 0)) {
                                foreach ($childfolders as $childfolder) {                       
                                    if (ftp_size($connection, $childfolder) == "-1") {
                                        //Folder, open and then add files to array.
                                        if(ftp_chdir($connection, $this-&gt;path.$folder."/".$subfolder."/".$childfolder)) {
                                            $folderobjects = ftp_nlist($connection, ".");

                                            foreach ($folderobjects as $folderobject) {
                                                $FinalObjectList[] = $this-&gt;path.$folder."/".$subfolder."/".$childfolder."/".$folderobject;
                                            }
                                        }

                                        else {
                                            print_r($this-&gt;path.$folder."/".$subfolder."/".$childfolder);
                                            echo "&lt;br /&gt;";
                                        }

                                    }
                                    else {
                                        $FinalObjectList[] = $FinalObjectList[] = $this-&gt;path.$folder."/".$subfolder."/".$childfolder;
                                    }                   
                                }
                            }
                        }
                        else{
                            print_r($this-&gt;path.$folder."/".$subfolder);
                            echo "&lt;br /&gt;";
                        }
                    }
                    else {
                        $FinalObjectList[] = $FinalObjectList[] = $this-&gt;path.$folder."/".$subfolder;
                    }   
                }
            }
        }       
        return $FinalObjectList;
    }

    public function index () {
        $SortedObjectList = array();

        $objectlist = $this-&gt;get_files_from_ftp();
        foreach($objectlist as $object) {
            $chunks = explode("_", $object);
            $strippath = explode("/", $chunks[0]);


            $SortedObjectList[end($strippath)] = $object;

            unset($chunks);
            unset($strippath);
        }

        var_dump(count($SortedObjectList));

    }
}
Verify the return values of all the ftp_chdir() calls. Otherwise, you might be way off, fetching the wrong stuff.

Excessive nesting is a problem. As in most programming tasks, here you have one goal, and many things that can go wrong along the way. You want to return early if an error occurs. A general pattern to follow is:

if (OK1 != ($result1 = operation1(…))) {
    return null; # or however you want to indicate failure
}
if (empty($result2 = operation2(…))) {
    return $result2; # nothing more to do
}
$retval = array('something involving' =&gt; $result2);
return $retval;


Note that all calls to functions that can fail occur within a if condition. The handler for the exceptional case follows immediately, and usually involves bailing out. Then the success codepath is linear and doesn't need to be nested.