Equivalent is_file () function for urls?

What is the best way to check if a given url is pointing to a valid file (i.e. not return 404/301 / etc)? I have a script that will load specific .js files on a page, but I need a way to check every URL that it gets in a valid file.

I am still following the PHP manual to find out which file functions (if any) will actually work with remote URLs. I will edit my post when I find more detailed information, but if someone has already gone down this path, do not hesitate to call.

+3
source share
3 answers

The get_contents file is slightly overfulfilling the target, as it is enough for the HTTP header to make a decision, so you need to use curl to do this:

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>
+5
source

one of such ways would be to request a URL and get a response with a status code of 200 back, besides this, there really is no good way, because the server has the ability to process the request, but it loves (including giving you other status codes for files that exist, but you don’t have access for a number of reasons).

0
source

If your server does not have fopen wrappers (any server with a decent degree of security will not), you will have to use the CURL functions .

0
source

Source: https://habr.com/ru/post/1698945/


All Articles