I am trying to simple run fopen() in the functions.php, and have also tried it in a test.php wordpress template file.
But it does not work. If I move the test.php file and csv file to a location outside the theme folder then it works first time.
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
echo '<pre>';
var_dump(csv_to_array('csv/nationality-codes.csv'));
echo '</pre>';
This is my folder structure in the theme file...
Any ideas why it does not work?
Finally got my code working with the help from NathanDawnson.
Some reason functions.php did not like relative path. This was the fix...
See full code working.
You need to use the full file path instead of a relative one.
Use the WordPress function
get_template_directory()
to get the path to your template directory. From there add the path to your file.Change:
To: