file_exists to check news

Asked

Viewed 68 times

0

Hello, I’m having problems with my code. His initial idea is to go through the server and show the links related 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 am running this code on my machine; but I have no knowledge if the command file_exists works to traverse external files.

  • What are you trying to do? The function file_exists does not work for Urls.

  • I didn’t know the function file_exists did not work for url’s, would like to know something you can check the link and if it exists

2 answers

1


The function file_exists works only for local archive.

It’s right to use the curl to make the connection to the desired page and check if the answer is valid.

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

  • 1

    Also remembering that there is always the possibility of the server to return a 301 redirect. In this case it would be necessary to follow the informed URL and then check if it exists.

  • Thanks @Rodrigo Rigotti with a good reading I was successful, it was relatively simpler than I thought, I just checked with the curl and created loop loop. Thanks anyway

0

file_exists works only for absolute paths, i.e., within the server filesystem. I advise checking whether file_get_contents returns code 200, not 404, for what you want to do.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.