【问题标题】:FTP connection through PHP always ask for username & password通过 PHP 的 FTP 连接总是要求输入用户名和密码
【发布时间】:2017-12-11 05:19:33
【问题描述】:

所以我有一个包含大量大文件(主要是卫星图像数据)的 FTP 服务器。我正在用 PHP 构建一个系统来服务用户下载文件。系统可以显示FTP服务器中的文件列表,以便他们下载文件。通常,列表不是来自 FTP 服务器,我将列表存储在数据库中。

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

我系统的流程是:

  1. 检查请求的数据是否可用。
  2. 如果可用,则使用ftp_connect 进行FTP 连接
  3. 如果连接成功,则使用ftp_login登录FTP
  4. 向数据库中的log表插入新记录。
  5. 使用 PHP header('Location:') 将用户重定向到请求的文件
  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>";
    } 
    ?>

【问题讨论】:

  • 与问题分享您的代码。当您使用ftp_login 登录时,您应该使用ftp_get 方法下载文件
  • 我已编辑问题并粘贴代码

标签: php ftp


【解决方案1】:

您需要使用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);

【讨论】:

  • 我刚刚收到警告:Warning: ftp_get(): ftp://172.xx.xx.xx/lisatstor01/LA3_2016-11-08_Muko.zip: No such file or directory
  • 其实http和ftp不在同一个服务器
  • @ArifKWijayanto 您的路径不正确,可能尝试仅使用路径'/lisatstor01/LA3_2016-11-08_Muko.zip'
【解决方案2】:

@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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2012-08-09
    • 2021-11-23
    相关资源
    最近更新 更多