不能使用php fopen()函数在wordpress functions.php [英] Cant use php fopen() function in wordpress functions.php

查看:228
本文介绍了不能使用php fopen()函数在wordpress functions.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 functions.php 中简单运行fopen(),并且还在 test.php wordpress模板文件中尝试过。

I am trying to simple run fopen() in the functions.php, and have also tried it in a test.php wordpress template file.

但它不工作。如果我将test.php文件和csv文件移动到主题文件夹之外的位置,那么它会第一次工作。

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...

>

推荐答案

您需要使用完整的文件路径而不是相对路径。

You need to use the full file path instead of a relative one.

使用WordPress函数 get_template_directory()获取模板目录的路径。

Use the WordPress function get_template_directory() to get the path to your template directory. From there add the path to your file.

更改:

var_dump(csv_to_array('csv/nationality-codes.csv'));

To:

var_dump( csv_to_array( get_template_directory() . 'csv/nationality-codes.csv' ) );

这篇关于不能使用php fopen()函数在wordpress functions.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆