簡體   English   中英

FTP 下載文件夾與 php

[英]FTP download folder with php

所以我想知道我可以從我的 FTP 服務器下載一個內部文件夾,只需單擊一個link 當我必須從我的 FTP 服務器下載我的.log文件時,我會這樣做:

首先我創建一個鏈接:

function FTP_download($id, $folder = ""){
    ?> <a href="ftp_download.php?folder=<?php echo $folder; ?>&id=<?php echo $id;?>&file=<?php echo $id.'.log'; ?>" download> Download full log </a> <?php
}

然后我調用下載器文件:

<?php
//Does GET exist
if (!isset($_GET['file'])) {
    echo"ERROR - loading log";
    return false;
}

//Define FTP variables
$ftp_server = "xxxx";
$ftp_user_name = "xxxx";
$ftp_user_pass = "xxxx";
$file = $_GET['file'];
$id = $_GET['id'];
$folder = $_GET['folder'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);


// Change dir to id folder
ftp_chdir($conn_id, $id);
// Change dir to logs
ftp_chdir($conn_id, $folder);

//Check if file exists
ftp_pasv($conn_id, true);
$list = ftp_nlist($conn_id, '');
if (!in_array($file, $list)) {
    return false;
}


// get the size of $file
$size = ftp_size($conn_id, $file);

//Create headers for force download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . $size);

//Read file content
readfile('ftp://'.$ftp_user_name.':'.urlencode($ftp_user_pass).'@'.$ftp_server.'/'.$id.'/'.$folder.'/'.$file);
ftp_close($conn_id);
return true;
?>

通過單擊該鏈接,將開始瀏覽器下載。

在此處輸入圖像描述

是否可以創建一個下載文件來下載整個文件夾,其中包含所有內容?

比如下載 -> 'ftp://'.$ftp_user_name.':'.urlencode($ftp_user_pass).'@'.$ftp_server.'/'.$id.'/'.$folder

我要做的是下載/tmp目錄中的所有文件,然后將 zip 一起下載到存檔中。 然后可以將該文件作為響應流式傳輸。

確保您的 tmp 目錄被清除,通常 php 在請求完成后會自行執行,請參見此處

PHP ZipArchive

如果您想在單個請求中下載所有文件(而不是在文件夾中)。
當使用“Content-Disposition”值作為文件內容之間的分隔符時,這可能會起作用。 我不能自信地向你解釋你需要什么。 也許在谷歌上搜索“多部分表單請求”會給你一個教程。

至於創建一個直接文件夾作為響應,我從未見過這樣的功能,也不認為這是可能的。

在我看來,Zip 是這里的最佳選擇,因為它應該得到足夠廣泛的支持,並保持你的文件夾完好無損,我認為你想保留它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM