function ftp_get_files($opts, $pipe, $cmd = __FUNCTION__) { # set prefix $prefix = $cmd; # merge opts $opts = merge_opts($opts, $pipe, 'files'); # get files opt $files = get_opt_config_value($prefix, $opts, 'files'); if (!check_opt_set_type($cmd, $files, 'files', 'array_of_strings')) { return false; } # get local dir opt $local_dir = get_opt_config_value($prefix, $opts, 'local_dir'); if (!check_opt_if_set_type($cmd, $local_dir, 'local_dir', 'string')) { return false; } # loop over files $res_files = array(); foreach ($files as $file) { # get the save_to_file value if ($local_dir) { $file_name = basename($file); $save_to_file = "{$local_dir}/{$file_name}"; } else { $save_to_file = $file; } # process request $req = array('file' => $file, 'save_to_file' => $save_to_file); $r = ftp_get_file($req, $opts, $cmd); if ($r === false) { return false; } # add file to return list $res_files[] = $save_to_file; } return array('files' => $res_files); }
function ftp_get_files($opts, $pipe = false, $cmd = __FUNCTION__) { # set prefix $prefix = 'ftp'; # merge opts $opts = merge_opts($opts, $pipe); # get execute opt $execute = get_opt_config_value($prefix, $opts, 'execute', true); if (!check_opt_set_type($cmd, $execute, 'execute', 'boolean')) { return false; } # check if we should execute or not if (!$execute) { return true; } # get files opt $files = get_opt($prefix, $opts, 'files'); if (!check_opt_set_type($cmd, $files, 'files', 'array')) { # array_of_[string|string_array_1|string_array_2] return false; } # setup connection $conn = ftp_get_conn($opts, $cmd); if (!$conn) { return false; } $svr = $conn['svr']; # download all the files $local_files = array(); $count = count($files); debug_echo($cmd, "downloading all files from FTP server ({$count} in total)"); foreach ($files as $file_pair) { # get the remote and local file names if (is_string($file_pair)) { $remote_file = $local_file = $file_pair; } elseif (is_array($file_pair)) { # TODO: better error handling here $remote_file = $file_pair[0]; if (!$remote_file) { return error($cmd, "remote file not set for FTP download"); } $local_file = $file_pair[1]; if (!$local_file) { $local_file = $remote_file; } } else { $type = gettype($file_pair); return error($cmd, "file defined for FTP transfer is of type '{$type}'"); } # download the file $opts['remote_file'] = $remote_file; $opts['local_file'] = $local_file; $r = ftp_get_file($opts, null, $cmd); if ($r === false) { return false; } $local_files[] = $local_file; } # return if ($pipe === false) { return $local_files; } return array('files' => $local_files); }