如果链接不存在,请尝试另一个

时间:2013-03-17 22:52:22

标签: php

我正在寻找检查链接是否在远程服务器上工作/存在的最快方法,如果没有尝试其他链接。类似于nginx中的“try_files”,仅用于链接...

例如:

try first 
header("Location:" . $VIDEO_1);
if there's no $VIDEO_1 try
header("Location:" . $VIDEO_2);
if there's no $VIDEO_2 try
header("Location:" . $VIDEO_3);

目前我正在使用一个检查大小的函数,并发送标题...但文件大小检查很慢

if($file_size > "9000000"){
        header("Content-type: video/x-flv");
        header("Location:" . $VIDEO . $dop);
}else{
        header("Content-type: video/x-flv");
        header("X-Accel-Redirect: /".$_GET["filename"].$dop);
}

1 个答案:

答案 0 :(得分:1)

由于它位于远程服务器上,您可以使用get_headers()

$header = get_headers("http://stackoverflow.com/users/flair/1401975.png");
preg_match('/\d{3}/', $header[0], $code); // Extracting the HTTP status code

if($code[0] < 400){ // Or maybe just $code[0] == 200 ? http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    echo 'EXISTS !!!';
}else{
    echo 'Doesn\'t exists';
}