2017-07-07 41 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

該文件已下載。但它已損壞/損壞。

相關問題