Search code examples
phpurlfile-exists

How to check a url exist or not in php


I want to check the url http://example.com/file.txt exist or not in php. How can I do it?


Solution

  • if(! @ file_get_contents('http://www.domain.com/file.txt')){
      echo 'path doesn't exist';
    }
    

    This is the easiest way to do it. If you are unfamiliar with the @, that will instruct the function to return false if it would have otherwise thrown an error