簡體   English   中英

如何使用csv中的php作為參考下載多個文件?

[英]How to download multiple files using php from csv as the references?

php的一種新功能,但我開始對此有所了解。

我想做的事...

我有一個csv,其中包含對我可以下載的文件的引用。

我找到此頁面: 如何通過php下載xml文件?

這使我能夠下載xml文件(如果我寫了我希望將其保存到的URL和目錄沒有問題)。

我如何修改此php以獲取csv中的所有xml文件?

我認為它將是:foreach和變量函數等,但不知道如何。

而且在csv中僅包含文件名,而不包含完整的url,但是url的第一部分將始終保持不變。 下載目錄也是如此。 我希望將所有文件下載到我選擇的相同目錄中,並且文件名將與一個即時消息下載名稱相同。

另外,如果例如現在我要下載圖像或任何其他文件類型,我將如何更改php? 我認為這會很容易嗎?

謝謝約翰的幫助

我將假定CSV文件如下所示:

“文件名”;“ URL”

“文件1”; “ URL到f1”

“文件2”; “ URL到f2”

“文件3”; “ URL到f3”

“文件4”; “ URL到f4”

“文件5”; “ URL到f5”

“文件6”; “ URL到f6”

因此列分隔符為; 和字符串分隔符"

因此代碼如下所示:

<?php

$fileContent = $file("path/to/csv/file"); // read the file to an array
array_shift( $fileContent ); // remove the first line, the header, from the array
foreach( $fileContent as $line ) {
    $columns = str_getcsv( $line, ";", '"' );
    $url = $columns[1]; // as suposed previously the url is in the second column
    $filename = $columns[0];
    downloadFile( $url, $filename );
}

function downloadFile( $url, $filename ) {
    $newfname = "/path/to/download/folder/" . $filename ;
    $file = fopen ($url, "rb");
    if ($file) {
        $newf = fopen ($newfname, "wb");
        if ($newf)
            while(!feof($file)) {
                fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
            }
    }
    if ($file) {
        fclose($file);
    }

    if ($newf) {
        fclose($newf);
    }
}

暫無
暫無

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

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