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?

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

1 Answer 1

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';
1
  • correct answer. Stupid mistake made by copy and paste :S. Thank you!
    – Leon
    Commented Mar 2, 2017 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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