Get all the PDF files from FTP server in different folders
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.