所以我有一个包含大量大文件的FTP服务器(主要是卫星图像数据)。我正在用PHP构建一个系统来为用户提供下载文件的服务。系统可以显示FTP服务器中的文件列表,因此可以下载文件。通常,列表不是来自FTP服务器,我将列表存储在数据库中。
对于FTP连接,我已将详细信息(ftp用户,ftp密码和ftp服务器)存储在配置文件中。所以在这里我不希望用户输入任何内容,只需单击下载按钮,然后获取文件。
我的系统流程是:
ftp_connect
ftp_login
实际上有效!
但它总是要求每个用户下载请求的FTP用户名和密码。
那有什么不对?或者有更好的方法吗?
编辑: 这是download.php中的代码:
<?php
include "config.php";
include "query_user.php";
include "function.php";
$data_id = $_GET['data_id'];
$user_id = $query_user['user_id'];
$query = mysqli_query($link, "SELECT count(*) as jumlah, data_url FROM lord_data WHERE data_id='".$data_id."'");
$query_data = mysqli_fetch_array($query);
if($query_data['jumlah'] > 0){
$name= $query_data['data_url'];
set_time_limit(20);
$conn_id = ftp_connect($ftp_server, 21, 10);
if($conn_id){
$conn_login = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$query = mysqli_query($link, "INSERT INTO lord_download_log (user_id, data_id) VALUES ('$user_id', '$data_id')");
header('Location: ftp://'.$ftp_server.'/foldername/'.$name.'');
// close the connection
ftp_close($conn_id);
}else{
$ftp_server = "172.xx.xx.xx";
$change_ip = ftp_connect($ftp_server, 21, 10);
if($change_ip){
$conn_login = ftp_login($change_ip, $ftp_user_name, $ftp_user_pass);
$query = mysqli_query($link, "INSERT INTO lord_download_log (user_id, data_id) VALUES ('$user_id', '$data_id')");
header('Location: ftp://'.$ftp_server.'/foldername/'.$name.'');
// close the connection
ftp_close($change_ip);
}else{
echo "Couldn't establish a connection.";
}
}
}else{
echo "<script>alert('Download failed! Data is not available');document.location='index.php?p=data_download';</script>";
}
?>
答案 0 :(得分:0)
您需要使用ftp_get
方法下载文件,而不是将用户重定向到文件位置。
使用以下代码从FTP获取文件并强制用户下载:
$server_file = "/path/to/server/file";
$conn_id = ftp_connect($ftp_server);
$local_file = tempnam(sys_get_temp_dir(), 'download_');
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
$quoted = sprintf('"%s"', addcslashes(basename($local_file), '"\\'));
$size = filesize($local_file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
}
ftp_close($conn_id);
答案 1 :(得分:0)
@ dileep-kumar现在有效!
我按如下方式更改代码:
<?php
include 'config.php';
// connect and login to FTP server
$conn_id = ftp_connect($ftp_server, 21, 10);
$login = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// turn passive mode on
ftp_pasv($conn_id, true);
if($login){
$chdir = ftp_chdir($conn_id, "lisatstor01");
$local_file = "local.zip";
$server_file = "LA3_2016-10-19_Lumajang.zip";
// download server file
if (ftp_nb_get($conn_id, $local_file, $server_file, FTP_BINARY))
{
$quoted = sprintf('"%s"', addcslashes(basename($local_file), '"\\'));
$size = filesize($local_file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
echo "Successfully written to $local_file.";
}
else
{
echo "Error downloading $server_file.";
}
}else{
echo "login failed";
}
// close connection
ftp_close($conn_id);
?>
我更改了文件所在的目录。我使用ftp_nb_get
代替简单ftp_get
。
下载文件。但它已损坏/损坏。