file_exists to verify news

0

Hello, I'm having problems with my code. His initial idea is to go through the server and show the links to the news.

<?php
$inicio=180;

if(file_exists("http://www.sitequalquer.com.br/noticia_".$inicio.".htm")){

      while($inicio){

        echo '<a href="http://www.sitequalquer.com.br/noticia_'.$inicio.'.htm">noticia'.$inicio.'</a><br>';

        $inicio++;

      }

    }else{

        echo "http://www.sitequalquer.com.br/noticia_".$inicio.".htm";
    }

?>

I'm running this code on my machine; but I'm not aware if the file_exists command works to traverse external files.

    
asked by anonymous 13.01.2015 / 19:43

2 answers

1

The function file_exists works only for local files.

The right thing is to use curl to connect to the desired page and check if the answer is valid.

Generally checking the status code of the response already resolves (for example, 404 for pages not found) but less elaborate sites may end up returning 200 for pages not found. In this case it is good to analyze the content of the page received.

    
13.01.2015 / 19:48
0

file_exists works only for absolute paths, ie within the server's filesystem. I'd advise checking if file_get_contents returns code 200, not 404, for what you want to do .

    
13.01.2015 / 19:47