質問

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>
';
}
役に立ちましたか?

解決

Here's one way to do this:

$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
    }
}

他のヒント

You could put them all into an array. Then while() the response was === false, array_shift the next one out of the array and use it. Once it's not false, you'll echo the last one that was used.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top