1

I'm trying to pull some data out of a csv file but I'm stuck because file_exists() says the file doesn't exist when used with get_template_directory_uri().

This is the short code:

function getPeopleNumber() {
    $csv = get_template_directory_uri().'/report/report.csv';
    $delimiter=',';
    if(!file_exists($csv) || !is_readable($csv)) {
        return FALSE;
    }
    $header = NULL;
    $data = array();
    if (($handle = fopen($csv, 'r')) !== FALSE) {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
            if(!$header) {
                $header = $row;
            }
            else $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }
    return $csv;
}

If I echo $csv there's the complete (and correct) path.

But still the function returns FALSE. What am I doing wrong?

  • maybe it's not readable by the web server? – Fayaz Mar 2 '17 at 10:34
  • @Fayaz well I've set all the permissions and both the file and webserver are on localhost – Leon Mar 2 '17 at 10:36
  • did you try with get_template_directory() as I suggested in the answer? – Fayaz Mar 2 '17 at 10:54
2

You are using get_template_directory_uri(), that's URL, not PATH.

Instead use get_template_directory(), that'll return PATH. Like:

$csv = get_template_directory() . '/report/report.csv';
| improve this answer | |
  • correct answer. Stupid mistake made by copy and paste :S. Thank you! – Leon Mar 2 '17 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.