我需要检查多个域/服务器中是否存在文件,然后向用户显示下载链接或写入错误消息。我有这个脚本适用于1个域:
<?php
$domain0='www.example.com';
$file=$_GET['file']
$resourceUrl = 'http://$domain0/$file';
$resourceExists = false;
$ch = curl_init($resourceUrl);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
//200 = OK
if ($statusCode == '200') {
$resourceExists = true;
}
if ($resourceExists == true) {
echo "Exist! $file";
} else {
echo "$file doesnt exist!";
}
?>
现在我需要检查该文件是否存在于4个域中,我该怎么做?我不知道如何使用数组,所以如果有人解释我这样做,我会非常感激。
答案 0 :(得分:0)
答案 1 :(得分:0)
我会调用一个函数来获得结果
function checkFileOnDomain($file,$domain) {
$resourceUrl = "http://$domain/$file";
$ch = curl_init($resourceUrl);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($statusCode == '200')
return true;
}
$file=$_GET["file"];
// $_GET should be sanitized!
$domain_list=array("www.test1.com","www.test2.com");
foreach ($domain_list as $domain) {
echo "Check DOMAIN: $domain <hr/>";
if (checkFileOnDomain($file,$domain)) {
echo ">> [ $file ] EXISTS";
} else {
echo ">> [ $file ] DOES NOT EXIST";
}
echo "<br/><br/>";
} unset($domain);
编辑:
要应用您的规范,您需要在foreach之前获得额外的变量。
$link_to_file="";
foreach ($domain_list as $domain) {
if (checkFileOnDomain($file,$domain)) {
$link_to_file="$domain/$file";
break; // get first result and quit
}
} unset($domain);
if (!empty($link_to_file)) {
echo $link_to_file; //file is here
} else {
echo "404";
}