How to use relative path in fopen?

Asked

Viewed 80 times

0

I have this Handle that reads a CSV:

<?php

$handle = fopen("/csv/listalistalevantamentoestoque.csv", "r");

$row = 0;
while ($line = fgetcsv($handle, 1000, ",")) {
    if ($row++ == 0) {
        continue;
    }

    $people[] = [
        'nome' => $line[0],
        'site' => $line[1],
        'telefone' => $line[2],
        'idade' => $line[3]
    ];
}

print_r($people);

fclose($handle);

?>

When I run Handle it returns this error saying that it did not find the file. Since this CSV in question is in a folder "csv" in the same directory of Handle php.

Exit:

Warning: fopen(/csv/listalevantamentoestoque.csv): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/folic/wp-content/themes/Avada/woocommerce/get-stock/handle.php on line 3
  • "in the same directory, "but you pointed to the folder relative to the system root. Using the toolbar at the beginning makes the path absolute from the root. If you want a folder in the same directory do ./csv/... or as appropriate __DIR__ . '/csv/...'.

  • php $handle = fopen(__DIR__ . "/csv/listalevantamentoestoque.csv", "r"); It worked! Thank :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.