【问题标题】:Cant use php fopen() function in wordpress functions.php无法在 wordpress functions.php 中使用 php fopen() 函数
【发布时间】:2014-02-24 14:22:17
【问题描述】:

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

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

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>';

这是我在主题文件中的文件夹结构...

任何想法为什么它不起作用?

【问题讨论】:

  • fopen 将在无法打开文件时触发警告。启用错误报告和/或检查您的错误日志。
  • 它可以很好地打开文件。只是不在主题文件夹中。没有错误
  • 在中间给出一些错误信息并检查它失败的地方。
  • @Joshc 启用调试,它会显示路径问题
  • 将 print_r 重命名为 var_dump 并检查函数返回的内容。您也可以尝试在函数调用之前设置错误报告。

标签: php wordpress csv fopen


【解决方案1】:

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

使用 WordPress 函数 get_template_directory() 获取模板目录的路径。从那里添加文件的路径。

变化:

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

收件人:

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

【讨论】:

    【解决方案2】:

    在 NathanDawnson 的帮助下,我的代码终于可以正常工作了。

    一些原因functions.php 不喜欢相对路径。这是解决办法...

    get_template_directory() . '/csv/nationality-codes.csv'
    

    查看完整的工作代码。

    // csv to array
    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;
    }
    
    
    // rider nationality
    function motocom_rider_nationality( $field )
    {
    
        // reset choices
        $field['choices'] = array();
    
        // get the textarea value from options page without any formatting
        $choices = csv_to_array( get_template_directory() . '/csv/nationality-codes.csv' );
    
        $field['choices'] = array(
            null => 'Select nationality...'
        ); 
    
        // loop through array and add to field 'choices'
        if( is_array($choices) )
        {
    
            foreach( $choices as $choice )
            {
    
                $label = $choice['Country'];
                $value = $choice['A3'];
    
                $field['choices'][ $value ] = $label . ' [' . $value . ']';
    
            }
        }
    
        // Important: return the field
        return $field;
    
    }
    add_filter('acf/load_field/name=rider_nationality', 'motocom_rider_nationality');
    

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 2014-08-26
      相关资源
      最近更新 更多