dongluo3962 2013-02-02 02:06
浏览 136

有没有办法循环遍历地址并在PHP中一次(或一个接一个)下载多个文件?

Okay, so, I have a website that has many different data sets available for download. Now, rather than going through and clicking each one individually and choosing to save each file to my PC, is there a way I can use PHP to loop through and force download all the files I want. I am the user in this case and have no access to server side. Most of the things I have read already are of people setting up their own website for other users to download things.

Details:

Now, I know that each file is located at say, blahblah.com/downloadset1, /downloadset2, ... , /downloadsetN

I can then set up a text box where I can enter each set number I want, separated by commas. For Example:[ 1, 2, 300, 4325 ]

From there I can have the script append the textbox values to the end of "blahblah.com/downloadset"

Which would give me an array of (blahblah.com/downloadset1, blahblah.com/downloadset2, blahblah.com/downloadset300, blahblah.com/downloadset4325)

Now those addresses have no content but the download itself, so if I would type them in my address bar, the download pop-up would just come up asking to save or open.

Can I write something into the script to make it loop through my new array of download addresses and start the downloads as "Save" without it asking me for each one?

  • 写回答

1条回答 默认 最新

  • dongxi7704 2013-02-02 02:50
    关注

    If you want to be able to save files, you must be running your script on the machine on which you can write. So let's assume you have php running on your local machine; then there are potentially two questions you need to answer.

    1) how do I make a list of names of the files I want to download?

    2) How do I download a file programmatically?

    It sounds from your question that you figured the first part out. I will cover just the second part - making a php script that downloads all the files in a list. There are two common techniques for this - curl (sometimes written cURL) or wget. You can find tons of information when you type "download file with curl" or "download file with wget", so I will just give a minimal example: this reads one image from http://www.floris.us/SO/IMG_0250.jpg , and saves a local copy at image1.jpg. If you save the script as curlTest.php, you would run it with

    php curlTest.php
    

    You should be able to go from here (build a loop, add diagnostics, error checking...)

    <?php
    $url = 'http://www.floris.us/SO/IMG_0250.jpg';
    $outputfile = "image1.jpg";
    $cmd = "wget -q \"$url\" -O $outputfile";
    exec($cmd);
    ?>
    

    If I have misinterpreted your setup, please let me know in the comments.

    评论

报告相同问题?