0

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?

2
Contributors
3
Replies
24
Views
3 Years
Discussion Span
Last Post by showman13
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.