0

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';

?>
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
03storic
  • 3
  • 2
  • Why are you comparing the `$_POST['url']` to a url in a file? Don't you want to check the existence of `$url` on a remote server? If thats the case my solution below is the only one that is checking a remote server. – PeppyHeppy Feb 03 '13 at 09:21

3 Answers3

0

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';
   }

}

?>
FreudianSlip
  • 2,870
  • 25
  • 24
  • I am getting an error on line 12 with the else operator why would this be happening? – 03storic Feb 03 '13 at 09:10
  • This is only checking that the url passed in as a `$_POST` parameter exists in the local file urls.txt. There isn't anything "remote" happening here. – PeppyHeppy Feb 03 '13 at 09:23
0

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>';
    }
  }  
}
?>
Ehs4n
  • 762
  • 1
  • 8
  • 24
0

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.
Community
  • 1
  • 1
PeppyHeppy
  • 1,345
  • 12
  • 20
  • You might want to refer to all of the available status codes to ensure that you don't misinterpret other status codes, etc: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html – PeppyHeppy Feb 03 '13 at 09:18