function ftp_downloadfile($directory, $entry) { // ------------------------------------------------------------------------- // Global variables // ------------------------------------------------------------------------- global $net2ftp_globals, $net2ftp_result, $net2ftp_settings; // ------------------------------------------------------------------------- // Get the file from the FTP server to the web server // ------------------------------------------------------------------------- // Open connection $conn_id = ftp_openconnection(); if ($net2ftp_result["success"] == false) { return false; } // Check if the filesize is smaller than the allowed filesize // $ftp_size_result = ftp_size($conn_id, "$directory/$entry"); // if ($ftp_size_result > $net2ftp_settings["max_filesize"]) { // $errormessage = __("The file is too big to be transferred"); // setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__); // return false; // } // FTP mode $ftpmode = ftpAsciiBinary($entry); // Temporary filename $tempfilename = tempnam($net2ftp_globals["application_tempdir"], "downl__"); if ($tempfilename == false) { @unlink($tempfilename); $errormessage = __("Unable to create the temporary file. Check the permissions of the %1\$s directory.", $net2ftp_globals["application_tempdir"]); setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__); return false; } registerTempfile("register", $tempfilename); // Get the file // ftp_getfile($conn_id, $localtargetdir, $localtargetfile, $remotesourcedir, $remotesourcefile, $ftpmode, $copymove) ftp_getfile($conn_id, "", $tempfilename, $directory, $entry, $ftpmode, "copy"); if ($net2ftp_result["success"] == false) { @unlink($tempfilename); $errormessage = __("Unable to get the file <b>%1\$s</b> from the FTP server and to save it as temporary file <b>%2\$s</b>.<br />Check the permissions of the %3\$s directory.<br />", $entry, $tempfilename, $net2ftp_globals["application_tempdir"]); setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__); return false; } // Close connection ftp_closeconnection($conn_id); // ------------------------------------------------------------------------- // Transfer temp file to browser // ------------------------------------------------------------------------- // Send the download headers - this function is in httpheaders.inc.php sendDownloadHeaders($entry, filesize($tempfilename)); // -------------------- // Open file // -------------------- // From the PHP manual: // Note: The mode may contain the letter 'b'. // This is useful only on systems which differentiate between binary and text // files (i.e. Windows. It's useless on Unix). If not needed, this will be // ignored. You are encouraged to include the 'b' flag in order to make your scripts // more portable. // Thanks to Malte for bringing this to my attention ! registerTempfile("register", $tempfilename); $handle = fopen($tempfilename, "rb"); if ($handle == false) { @unlink($tempfilename); $errormessage = __("Unable to open the temporary file. Check the permissions of the %1\$s directory.", $net2ftp_globals["application_tempdir"]); setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__); return false; } // -------------------- // Send file to browser // -------------------- // Old method: fpassthru. Avoid using fpassthru, as it reads the entire file into memory! // $success1 = fpassthru($handle); // if ($success1 == false) { // @fclose($handle); // @unlink($tempfilename); // $errormessage = __("Unable to send the file to the browser"); // setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__); // return false; // } // New method: read the file piece by piece and send it to the browser. while (!feof($handle)) { $buffer = fread($handle, 2048); echo $buffer; } // -------------------- // Close file // -------------------- $success2 = @fclose($handle); // -------------------- // Delete the temporary file // -------------------- $success3 = @unlink($tempfilename); if ($success3 == false) { $errormessage = __("Unable to delete the temporary file"); setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__); return false; } registerTempfile("unregister", $tempfilename); }
function net2ftp_module_printBody() { // -------------- // This function prints the unzip screen // -------------- // ------------------------------------------------------------------------- // Global variables // ------------------------------------------------------------------------- global $net2ftp_settings, $net2ftp_globals, $net2ftp_messages, $net2ftp_result, $net2ftp_output; if (isset($_POST["list"]) == true) { $list = getSelectedEntries($_POST["list"]); } else { $list = ""; } // ------------------------------------------------------------------------- // Variables for all screens // ------------------------------------------------------------------------- // Title $title = __("Unzip archives"); // Form name, back and forward buttons $formname = "UnzipForm"; $back_onclick = "document.forms['" . $formname . "'].state.value='browse';document.forms['" . $formname . "'].state2.value='main';document.forms['" . $formname . "'].submit();"; $forward_onclick = "document.forms['" . $formname . "'].submit();"; // ------------------------------------------------------------------------- // Variables for screen 1 // ------------------------------------------------------------------------- if ($net2ftp_globals["screen"] == 1) { // Next screen $nextscreen = 2; } elseif ($net2ftp_globals["screen"] == 2) { $net2ftp_output["unzip"] = array(); $net2ftp_output["ftp_unziptransferfiles"] = array(); // --------------------------------------- // Initialize variables // --------------------------------------- $moved_ok = 0; // Index of the archives that have been treated successfully $moved_notok = 0; // Index of the archives that have been treated unsuccessfully // --------------------------------------- // Open connection to the FTP server // --------------------------------------- setStatus(2, 10, __("Connecting to the FTP server")); $conn_id = ftp_openconnection(); if ($net2ftp_result["success"] == false) { return false; } // --------------------------------------- // Get the archives from the FTP server // --------------------------------------- for ($i = 1; $i <= $list["stats"]["files"]["total_number"]; $i = $i + 1) { // Set the status $message = __("Getting archive %1\$s of %2\$s from the FTP server", $i, $list["stats"]["files"]["total_number"]); setStatus($i, $list["stats"]["files"]["total_number"], $message); // Get the archive from the FTP server $localtargetdir = $net2ftp_globals["application_tempdir"]; $localtargetfile = $list["files"][$i]["dirfilename"] . ".txt"; $remotesourcedir = $net2ftp_globals["directory"]; $remotesourcefile = $list["files"][$i]["dirfilename"]; $ftpmode = ftpAsciiBinary($list["files"][$i]["dirfilename"]); $copymove = "copy"; ftp_getfile($conn_id, $localtargetdir, $localtargetfile, $remotesourcedir, $remotesourcefile, $ftpmode, $copymove); if ($net2ftp_result["success"] == false) { setErrorVars(true, "", "", "", ""); $net2ftp_output["unzip"][] = __("Unable to get the archive <b>%1\$s</b> from the FTP server", htmlEncode2($list["files"][$i]["dirfilename"])); $moved_notok = $moved_notok + 1; continue; } // Register the temporary file registerTempfile("register", glueDirectories($localtargetdir, $localtargetfile)); // Enter the temporary filename and the real filename in the array $moved_ok = $moved_ok + 1; $acceptedArchivesArray[$moved_ok]["name"] = $list["files"][$i]["dirfilename"]; $acceptedArchivesArray[$moved_ok]["tmp_name"] = glueDirectories($localtargetdir, $localtargetfile); $acceptedArchivesArray[$moved_ok]["targetdirectory"] = $list["files"][$i]["targetdirectory"]; $acceptedArchivesArray[$moved_ok]["use_folder_names"] = $list["files"][$i]["use_folder_names"]; } // end for // --------------------------------------- // Unzip archives and transfer the files (create subdirectories if needed) // --------------------------------------- if (isset($acceptedArchivesArray) == true && sizeof($acceptedArchivesArray) > 0) { ftp_unziptransferfiles($acceptedArchivesArray); $net2ftp_output["unzip"] = $net2ftp_output["unzip"] + $net2ftp_output["ftp_unziptransferfiles"]; if ($net2ftp_result["success"] == false) { return false; } } // --------------------------------------- // Close the connection to the FTP server // --------------------------------------- ftp_closeconnection($conn_id); } // end elseif // ------------------------------------------------------------------------- // Print the output // ------------------------------------------------------------------------- require_once $net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/manage.template.php"; }