Download prompt for ftp_get

2019-09-16 09:08发布

I am currently implementing a system for users to download files from the ftp without the actual need of a ftp client. I have already developed a login system and have successfully managed to list out the files from the folder. However, I intend to make the download process using ftp_get with a "save as" prompt.

I have referred to http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_23210157.html#mainSection but the solution stated wasn't conclusive.

I did use one of the suggestion in the previous forum topic, but I got a msg "There was a problem" after executing ftp_get.

I will really appreciate with any help given ! Thanks !

    //--------------------------- Connects to FTP host ---------------------------------------------
$conn_id = ftp_connect($_COOKIE["process-1"]) or die("Couldn't connect to FTP server");
$login_result = ftp_login($conn_id, $_COOKIE["process-2"], $_COOKIE["process-3"]);
//--------------------------- Validates FTP Logiin (username,password) -------------------------
if(!$login_result) die("FTP Login failed. Contact support @ customercare@strategyinstitute.com");
ftp_pasv($conn_id,true);

$current_dir = ftp_pwd($conn_id);
$chdir = ftp_chdir($conn_id,$_COOKIE["process-5"]);
if(!$chdir) echo "Change Directory failed. Origin = " .$current_dir. "; Destination: ".$_COOKIE["process-5"];

echo ftp_pwd($conn_id) ."<br>";

$server_file = $_GET['file'];

$fp = fopen('php://stdout', 'w+');
if (ftp_get($conn_id, $fp, $server_file, FTP_BINARY)) {
        echo "Successfully written to $local_file\n";
} else {
        echo "There was a problem\n";
}

ftp_close($conn_id);

标签: php ftp
2条回答
可以哭但决不认输i
2楼-- · 2019-09-16 09:10

every browser pretty much is already an ftp client. Why are you reinventing the wheel?

Go to any navigable ftp site in your browser by typing ftp://browsableftpsite.com in the URL area.

查看更多
对你真心纯属浪费
3楼-- · 2019-09-16 09:23
// make the browser download anything you output
header('Content-type: file/binary');

// set the correct filename
header('Content-Disposition: attachment; filename="'.$file.'"'); 
查看更多
登录 后发表回答