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 = 'http://24houredocs.com/enroll_updates/TEST_24houredocs_STANDARD_20140210.csv';

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?

Recommended Answers

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. …

Jump to Post

All 3 Replies

OK, tried another way...

$url='http://24houredocs.com/enroll_updates/TEST_24houredocs_STANDARD_20140210.csv';


// 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???

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("http://www.24houredocs.com/enroll_updates/TEST_24houredocs_STANDARD_20140210.csv");

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 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.