This is baffling me and I am tired of trying different variations to accomplish what should be such a simple task.

I have a folder in the main directory of the domainb called 'enroll_updates' that a file will be written to via CRON every night.

But I am going to put in a safety check to ensure that the file hasn't already been created for that day. (shouldn't ever happen, but just in case)

All I need to do is find out if that particular .csv file exists in that folder.

This is what I have: But it always tells me that the file exists, even when I change the name in the query to something that doesn't.

I actually have two of the attempts included in this test script, one using curl and the other checking if the headers exist.

<?php

function url_exists($url) {
  if (!$fp = curl_init($url)){
    return false;
  }else{
    return true;
  }
}

$url = '';

print"exists : ".url_exists($url);



$file_headers = @get_headers($url);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    print"<br>headers don't exist";
  }else{
    print"<br>headers do exist";
  }
?>

The results print as

exists : 1
and
headers do exist

meaning the file exists and the headers exist...

But I can delete that .csv file from the folder and still get the same results.

So, what am I doing wrong?

Any ideas?

Dani AI

Generated

Nice resolution from in Post #4 — a HEAD-style cURL check is the right approach for an HTTP URL. A few clarifying points that explain why the earlier tests looked like “the file exists” even after deletion:

fopen on an http:// URL only needs a successful TCP connection; it will return a stream resource even when the server responds with an HTTP 404, so fopen() is not a reliable existence test for remote files. getheaders() returns raw status lines that vary (HTTP/1.0 vs HTTP/1.1, redirects, etc.), so comparing the entire string to 'HTTP/1.1 404 Not Found' is brittle. As noted, PHP file* functions (file_exists, is_readable) apply to filesystem paths, not URLs.

When the script runs on the same server, prefer a filesystem check. Example:

$path = $_SERVER['DOCUMENT_ROOT'] . '/enroll_updates/TEST_24houredocs_STANDARD_20140210.csv';
if (file_exists($path)) {
    // present
} else {
    // missing
}

For HTTP-accessible files use a robust status-code check. get_headers can be parsed for the numeric code, for example:

$hdrs = @get_headers($url);
if ($hdrs !== false && preg_match('/\s(\d{3})\s/', $hdrs[0], $m)) {
    $code = (int)$m[1];
    // treat 2xx–3xx as present; 4xx–5xx as missing/error
}

cURL is more reliable: issue a HEAD request, enable FOLLOWLOCATION, check the numeric HTTP code, and fall back to a tiny GET (Range: bytes=0-0) if the server rejects HEAD (405). Also handle redirects and timeouts.

Troubleshooting checklist: confirm the server isn’t returning a custom 404 page with HTTP 200, check CDN/proxy caching (append a cache-busting query string while testing), inspect server access logs, and use filesystem checks when possible to avoid HTTP-layer surprises.

Recommended Answers

All 3 Replies

OK, tried another way...

$url='';


// Check to see if the file exists by trying to open it for read only
if (fopen($url, "r")) {

print "<br><br>File Exists";

} else {

print "<br><br>Can't Connect to File";

}

and it prints out File Exists - even when I change the file name to something non-existant

Any Suggestions???

Member Avatar for Member #46692

A few things to try:

http://stackoverflow.com/questions/4253487/check-if-file-exists-in-php

Someone^^ is saying the function doesn't work for url paths. Only paths on the server.

Other thing, check you are changing the name of the right file. The number of times I've changed a file in a different folder only to realise my mistake an hour or two later!!

OK, found a cURL answer to the issue...

If this provides a $retcode of 200, then the file already exists, otherwise it doesn't...

Pretty simple, and it only took me 3 days to figure it out LOL
(actually I tried this a couple times before, but for some reason I thought that 400 meant it existed... Just totally confused myself.)

$ch = curl_init("");

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode >= 400 -> not found, $retcode = 200, found.
curl_close($ch);
if($retcode=='200'){
  print"<br>Success - retcode is : ".$retcode."<br>";
}else{
  print"<br>FAILURE - retcode is : ".$retcode."<br>";
}

I'm going to make this Solved, because it is, but going to open a new question that I really need some help with, so stay tuned...

Thanks,
Douglas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.