簡體   English   中英

使用php從ftp下載文件

[英]download file from ftp using php

我正在嘗試制作一個應用程序,管理員可以在該應用程序中查看特定文件夾(預定義)中的文件,並且應該能夠一次下載它們。 文件將存儲在服務器上。 有人可以指導我該怎么做嗎?

您是否要獲取遠程FTP服務器中的文件列表? 如果是這樣,則您需要ftp擴展名或使用PEAR的Net-FTP2擴展名(可悲的是它已被放棄,但是您可以從那里獲取一些代碼)

至於將來自FTP服務器的特定文件提供給遠程用戶,我建議使用readfile函數

readfile('ftp://server/file.txt');

請記住,您還需要添加適當的標題,例如Content-Type,Content-Length等,readfile手冊頁中有一些示例。

我知道這是一個古老的問題,但是我正在尋找類似的解決方案,因此我認為這對它的工作很有用。 我希望它可以幫助某人!

    <?php

//ftp-server
$connection_id = ftp_connect("here put the link of FTP server");
$ftp_username = "here the username";
$ftp_password = "here the password";

$file_path_my_pc = "here the path that you want to save your file..";
$file_path_ftp_server = "here the path of the file in FTP server/the name of file.csv";




$login = ftp_login($connection_id, $ftp_username, $ftp_password);

if (!$login) {
    echo "Connection to ftp server has failed!! ";
    exit;
} else {
    echo "Connected has be done!!";
}

ftp_pasv($connection_id, true);

if (ftp_get($connection_id, $file_path_my_pc.'/here the name of your file in the FTP server', $file_path_ftp_server, FTP_ASCII)) {

    echo "File has been downloaded!!";
    ftp_close($connection_id);
    return true;

} else {
    ftp_close($connection_id);
    echo "fail ... ";
    echo "Connected has be stopped!!";
    return false;

}

檢查file_get_contents函數。 它可以滿足您的需求。

例如:

<?php $file_content = file_get_contents ('ftp://mysite.com/file.txt'); ?>

然后,您只需要使用file_put_contentsfwrite (根據需要)將內容寫入文件系統即可。

暫無
暫無

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

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