PHP: download any file from ftp-server to harddrive? -


I really need your help, I'm successfully establishing a connection to my FTP server though I can not understand that How do I download a dynamic file from my server? I think this will help many other people because I did not find any tutorials or explanations anywhere on the web.

I'm just listing all my files on my FTP. If I click on one then I call at download.php which connects to the server and downloads the file to my hard drive. I was automatically able to prompt a download window and a file was downloaded on my HDD However, there is only one damaged file where no header is set.

  / * Path to files on FTP server ... Examples / folders / folders / name_of_file JPG * / $ p_arr = Explosion ("/", $ path); / * File name ... e.g. Name_of_file.jpg * / $ file = end ($ p_arr); // $ finfo = finfo_open (FILEINFO_MIME_TYPE); // returns a serious error - the function is not found // $ mimetype = finfo_file ($ $ finfo, $ file); // finfo_close ($ finfo); // filetype ($ file) / * * / $ tempFile = tempnam ("/ tmp", "FOO") to create a temporyry file; If (ftp_get ($ conn_id, $ tempfile, $ file, FTP_BINARY)) {/ * header content type: must be dynamic / header ('content-type:'. $ Mime type); / * Header to auto_proputav download window * / header ('content-dispute: attachment; filename =' '. $ File.' '' ''); Readfile ($ tempfile); } And {echo "had a problem 
"; Echo $ file "& lt; br & gt;"; //e.g. Image.jpg} / * Delete file after operation * / // unlink ($ tempFile);

1.) I can not understand why the ftp_get return is incorrect. The connection is established and the file is present in the correct directory. 2.) I do not know how I can get the mimetype of the file on the server and give it the downloaded file, so it is not damaged.

Please help me out here, I'm really stuck. Thank you in advance.

First try to save it locally and then press it back in the browser.

Use that code to save your file locally

  & lt ;? Php // defines some variables $ folder_path = "Your Fadder Path"; $ Local_file = "Local file path"; $ Server_file = "server file path"; // - Connection settings $ ftp_server = "IP address"; // FTP server address $ Ftp_user_name = "USERNAME"; // username $ ftp_user_pass = "PASSWORD"; // password # $ destination_file = "FILEPATH"; // Set the original connection $ conn_id = ftp_connect ($ ftp_server); // Login with username and password $ login_result = ftp_login ($ conn_id, $ ftp_user_name, $ ftp_user_pass); // $ server_file Try downloading and saving to $ local_file (ftp_get ($ conn_id, $ local_file, $ server_file, FTP_BINARY)) {resonant "successfully written at $ local_file \ n"; } Else {echo "was a problem \ n"; } // close the connection ftp_close ($ conn_id); ? & Gt;  

I also found it, maybe it could help you

My FTP server always kept me "bool" instead of providing me a directory, Responded to- list. I had to add ftp_pasv ($ conn_id, true); $ Login_result = just after ftp_login (...); Line. After that it worked fine.


Comments