Check if HTML page exists

I want to check if an HTML page exists on another site. More precisely, my users need to upload the .html page to their site, after which they need to click "confirm now" on my site. My site should then confirm that the html page exists on their site.

I found this code:

 $url = 'http://domain.tld/my_page.html'; $handle = @fopen($url,'r'); if($handle !== false){ echo 'Page Exists'; } else { echo 'Page Not Found'; } 

But if my_page.html does not exist, this code only returns "Exists Page".

+1
php fopen
source share
3 answers

Assuming you have the curl extension installed in your PHP, I think this is by far your best option: An easy way to check the URL for 404 in PHP?

+1
source share

EDIT 1 : sorry for edditing ... don't put the question first.

you need to understand that the server will respond in some way. you just need to check then the response code, not the response text. or to parse text.

in your case, the server response, for example:

 Object not found! The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again. If you think this is a server error, please contact the webmaster. Error 404 work.local Thu Jul 28 13:22:49 2011 Apache/2.2.15 (Linux/SUSE) 

Edit 2 : you can better use socket_connect instead of fopen to check if the file exists

+2
source share

The web server returns an HTTP 404 status code if the resource does not exist. Resource is the html file in your question.

You can make a chapter request to the server to find out about the status, or just get a request.

PHP also has a built-in function for this operation, it is called get_headers ( Demo ):

 $headers = get_headers($url, 1); $statusLine = $headers[0]; list(,$statusCode) = explode(' ', $statusLine, 3); 
+1
source share

All Articles