php - Check if file exist in multiple domains -


i need check if file exist in multiple domains/servers , show download link user or write error message. have script working 1 domain:

<?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!";   } ?>  

now need check if file exist in 4 domains, how can this? don't know how use arrays, maybe if explain me ho this, i'll grateful.

  1. i create array domains
  2. i loop through array "foreach"
  3. i call function result

    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 sanitized!  $domain_list=array("www.test1.com","www.test2.com");    foreach ($domain_list $domain) {     echo "check domain: $domain <hr/>";      if (checkfileondomain($file,$domain)) {         echo ">> [ $file ] exists";     } else {         echo ">> [ $file ] not exist";     }     echo "<br/><br/>"; } unset($domain); 

edit:

to apply specifications, need variable before foreach.

    $link_to_file="";     foreach ($domain_list $domain) {          if (checkfileondomain($file,$domain)) {             $link_to_file="$domain/$file";             break; // first result , quit         }      } unset($domain);      if (!empty($link_to_file)) {             echo $link_to_file; //file here     } else {             echo "404";     } 

Comments