Viewed   109 times

I need to check if a particular file exists on a remote server. Using is_file() and file_exists() doesn't work. Any ideas how to do this quickly and easily?



You have to use CURL

function does_url_exists($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($code == 200) {
        $status = true;
    } else {
        $status = false;
    return $status;
Saturday, October 8, 2022

The PHP/CURL binding supports the CURLOPT_READFUNCTION option, which allows you to pass data to send chunk-by-chunk using that callback.

Pretty much exactly the same logic as is shown in this C example:

Sunday, October 23, 2022
$url = '';

// using file_get_contents function
$content = file_get_contents($url);
echo $content;
#output# "who is the Rector of the University"

// using file function // read line by line in array
$content = file($url);

#output# Array (0] => ?"who is the Rector of the University")

// using cURL
$ch = curl_init($url);  
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$content = curl_exec($ch);
echo $content;
#output# "who is the Rector of the University"
Wednesday, November 30, 2022

This is a way to see if any XML-files exists in that folder, yes.

To check for specific files use File.Exists(path), which will return a boolean indicating wheter the file at path exists.

Saturday, November 5, 2022

WebClient is fairly limited; if you switch to using WebRequest, then you gain the ability to send an HTTP HEAD request. When you issue the request, you should either get an error (if the file is missing), or a WebResponse with a valid ContentLength property.

Edit: Example code:

WebRequest request = WebRequest.Create(new Uri(""));
request.Method = "HEAD";

using(WebResponse response = request.GetResponse()) {
   Console.WriteLine("{0} {1}", response.ContentLength, response.ContentType);
Friday, November 18, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :