0

I want a Mirror script which determines whether or not a certain file is accessible or not. If the mirror 1 is accessible, it will present the link to mirror 1. If mirror 1 is not accessible, it will present the link to mirror 2.

This so far is perfect, however I would like more mirrors. For example, if mirror 2 is unavailable, mirror 3 will be presented. If mirror 3 is unavailable, mirror 4 will be presented and so on. I'm just not quite sure exactly how I could make this work. Has anyone got any suggestions? They would be highly appreciated. I've tried quite a lot of things already!

$mirror1 = "download1.exe";
$mirror2 = "download2.exe";
$header_response = get_headers($mirror1, 1);
if ( strpos( $header_response[0], "404" ) !== false )
{
    echo '
<a href="', $mirror1, '">Download Mirror 1</a>
';
} 
else 
{
    echo '
<a href="', $mirror2, '">Download Mirror 2</a>
';
}
4

2 回答 2

1

这是执行此操作的一种方法:

$mirrors = array("download1.exe","download2.exe","download3.exe");

foreach($mirrors as $mirror) {
    $header_response = get_headers($mirror, 1);
    if (strpos( $header_response[0], "404" ) === false) {
        echo '<a href="', $mirror, '">Download from '.$mirror.'</a>';
        break; //removing break will show all available mirrors
    }
}
于 2013-06-08T23:19:47.450 回答
0

您可以将它们全部放入一个数组中。然后 while() 响应为 === false,array_shift 将下一个移出数组并使用它。一旦它不是假的,你就会回显最后一个使用的。

于 2013-06-08T23:20:34.753 回答