简体   繁体   中英

How do I modify this code to check for file existence on a remote host?

I'm attempting to check for the existence of multiple files on a single website using the code below and am encountering a problem that only the top URL is being tested and even if it is a valid URL I still get URL doesn't exist

How would I modify the code to correctly return the results and check all the given url's within the text-file.

<?php 
$site = "http://site.com"
$urls = file('urls.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$found = false;
foreach($urls as $url)
   if($_POST['url'] == $site . $url)
      $found = true;

if($found)
   echo "URL exists";
else
   echo 'URL doesn\'t exist';

?>

A little bit of a logic change - customise to your needs.

<?php 

$site = "http://site.com"
$urls = file('urls.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($urls as $url)

   // TEST URL EXISTENCE HERE (not sure if just looking at $_POST will tell you if its a remote url?

   if($_POST['url'] == $site . $url) {
       echo "URL exists";
   } else {
       echo 'URL doesn\'t exist';
   }

}

?>

Try this. Note you may have to skip line endings, so use rtim(). Also if you want the urls.txt to test against multiple Input urls, the script will accomplish that as well.

<?php 
$site = "http://site.com"
$urls = file('urls.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($urls as $url) {
  //if you want to test multiple input urls, they might be in input array, say url[]
  //we can check for the array here
  if(is_array($_POST['url'])) {
    foreach($_POST['url'] as $post_url) {
      //You may want to skip line endings, so use rtrim
      if($post_url == ($site . rtrim($url)) {
        print 'Url found - '.$post_url.'<br>';
      } else {
        print 'Url not found - '.$post_url.'<br>';
      }
    }
  } else {
    //You may want to skip line endings, so use rtrim
    if($POST['url'] == ($site . rtrim($url)) {
      print 'Url found - '.$POST['url'].'<br>';
    } else {
      print 'Url not found - '.$POST['url'].'<br>';
    }
  }  
}
?>

Code that will check a list of urls on a remote server:

<?php 
$site = "http://site.com"
$urls = file('urls.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($urls as $url) {
  $headers = get_headers($site . $url, 1);
  $status_parts = explode(" ", $headers[0]);
  $status_code = $status_parts[1];
   if ($status_code == 200)
     echo "URL exists";
   else if ($status_code == 404)
     echo 'URL doesn\'t exist';
   else
     // error or something else?
}
?>

A couple of things to note:

  1. There are similar questions
  2. You might want to log the url int eh response instead of just outputting whether or not it exists or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM