1<?PHP2 functionLogftpdownload ($filename){3 $phpftp _host= "Host";//Server Address4 $phpftp _port= port;//port number of the server5 $phpftp _user= "username";//User name6 $phpftp _passwd= "password;//Password7 $ftp _path=dirname($filename) . "/";//get the road warp8 $select _file=basename($filename);//Get file name9 $ftp=Ftp_connect($phpftp _host,$phpftp _port);//connecting to an FTP serverTen if($ftp){ One if(Ftp_login($ftp,$phpftp _user,$phpftp _passwd)){//Login A if(@Ftp_chdir($ftp,$ftp _path)){//Enter the specified path - $tmpfile=Tempnam(GETCWD()." /"," temp ");//Create a unique temporary file this directory should have Read permission, or higher - if(Ftp_get($ftp,$tmpfile,$select _file, ftp_binary)) {//download the specified file to the temp file the Ftp_quit($ftp);//Close Connection - Header("Content-type:application/octet-stream"); - Header("Content-disposition:attachment; Filename= ".$select _file); - ReadFile($tmpfile); + unlink($tmpfile);//Delete temporary files - return true; +}Else{ A return false; at } -}Else{ - return false; - } -}Else{ - return false; in } - } to}
This is the upload function, first ready! It seems that I was from which the great God to find. Don't remember, sorry!
1 <? PHP 2 $path=$_get[' Timepath ']. ' /‘. $_get [' CompanyID ']. ' /‘. $_get [' Timepath ']. ' -‘. $_get [' Shopid ']. TXT '; 3 $downResult=logftpdownload ($path);
Stitch the path string, call the function, and pass in the path string!
Here, it's almost done! However, this is the ideal state, that is, the page does not have any output, the path $path is written dead, because the header (), can not have any output.
This situation is generally used with JS to solve, the front page with Ajax asynchronous submission to the background query related, processing data, if the file path is relatively single, even can be combined with a good path $path, my due to too many conditions, the file path is not the same, so only the data processing good, back to the front desk, Ajax judgment callback, if successful, use window.open (); Open a new window and pass the relevant parameters to the background controller, of course, preferably openwindow=window.open (); Assign it to close, and then give a intervalid =window.setinterval ("function name", 1000); This is the function named after opening a new window 1 seconds later, this function is used to display the exception thrown in the background, such as the file does not exist, FTP login, connection failure and so on.
This is probably the case, I am a small rookie, write this is to organize their own ideas, deepen the impression! Also hope that the gods come to guide you.
FTP File Download full code