php - Unable to make a file auto-download from an FTP server when script is run -
i attempting write php page takes variable filename within ftp , download it. however, not seem work. function (ftp_get) returning true echo statement being run, nothing else happens , there no errors in console.
<?php $file = $_get['file']; $ftp_server = "127.0.0.1"; $ftp_user_name = "user"; $ftp_user_pass = "pass"; // set connection or die $conn_id = ftp_connect($ftp_server) or die("couldn't connect $ftp_server"); // login username , password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if (ftp_get($conn_id, $file, $file, ftp_binary)) { echo "successfully written $file\n"; } else { echo "there problem\n"; } ?>
ideally, link them to: ftp://example.com/testfile.txt , download file them, however, shows them contents of file in browser rather downloading it.
i've gone through php manual site reading ftp functions , believe ftp_get correct 1 i'm suppose using.
is there potentially easier way of doing this, or i'm overlooking?
there 2 (or maybe more) ways can this. store copy of file on server ftp_get , send user afterwards. or download every time.
now can using ftp commands, there quicker way using readfile
.
following first example readfile
documentation:
// save file url $file = "ftp://{$ftp_user_name}:{$ftp_user_pass}@{$ftp_server}" . $_get['file']; // set appropriate headers file transfer header('content-description: file transfer'); header('content-type: application/octet-stream'); header('content-disposition: attachment; filename=' . basename($file)); header('content-transfer-encoding: binary'); header('expires: 0'); header('cache-control: must-revalidate'); header('pragma: public'); header('content-length: ' . filesize($file)); // , send them ob_clean(); flush(); // send file readfile($file);
this fetch file , forward it's contents user. , header make browser save file download.
and take further. let's save in file called script.php in directory accessible user via http://example.com/ftp/
. if using apache2 , have mod_rewrite enabled can create .htaccess
file in directory containing:
rewriteengine on rewriterule ^(.*)$ script.php?file=$1 [l]
when user navigates http://exmaple.com/ftp/readme.md
script.php file called $_get['file']
equal /readme.md
, file ftp://user:pass@ftp.example.com/readme.md
downloaded on computer.
Comments
Post a Comment