2017-07-07 35 views
0

所以我有一个大型文件(主要是卫星图像数据)的FTP服务器。我正在用PHP构建一个系统来为用户提供下载文件的服务。系统可以显示FTP服务器中的文件列表,以便他们可以下载文件。通常,该列表不是来自FTP服务器,我将该列表存储在数据库中。通过PHP的FTP连接总是要求用户名和密码

对于FTP连接,我在配置文件中存储了详细信息(ftp用户,ftp密码和ftp服务器)。所以在这里我不希望用户输入任何东西,只需点击下载按钮,并获取文件。

我的系统的流程是:

  1. 检查所请求的数据是可用的。
  2. 如果有的话,那么做ftp_connect
  3. 如果连接成功,FTP连接,然后登录使用ftp_login
  4. 插入新的记录在数据库中的日志表到FTP。
  5. 重定向用户请求的文件中使用PHP头( '位置:')
  6. 关闭FTP连接

它的实际工作!

但它总是要求每个用户下载请求的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

与问题分享您的代码。当使用'ftp_login'登录时,您应该使用'ftp_get'方法下载文件 –

+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); 
+0

我刚刚收到警告: 'Warning:ftp_get():ftp://172.xx。 xx.xx/lisatstor01/LA3_2016-11-08_Muko.zip:没有这样的文件或目录# –

+0

其实,http和ftp不在同一台服务器 –

+0

@ArifKWijayanto你的路径不正确,也许尝试使用路径''/仅限lisatstor01/LA3_2016-11-08_Muko.zip''。 –

0

@迪利普 - 库马尔它现在!

我更改代码如下:

<?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

该文件已下载。但它已损坏/损坏。

相关问题