【问题标题】:My function to create a csv file in php goes through but doesnt create the file我在 php 中创建 csv 文件的功能通过但不创建文件
【发布时间】:2019-12-06 11:28:45
【问题描述】:

大家好,这是我的代码。

        function markenbuero_csv() {

        // open the file "demosaved.csv" for writing
        $file = fopen('demosaved.csv', 'w');

        // save the column headers
        fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));

        // Sample data. This can be fetched from mysql too
        $data = array(
            array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
            array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
            array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'),
            array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'),
            array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55')
        );

        // save each row of the data
        foreach ($data as $row)
        {
        fputcsv($file, $row);
        }

        // Close the file
        fclose($file);  
        echo 'worked';
    }

我通过我的 Wordpress 自定义插件页面中的表单调用它,它会呼应“工作”,但不会在我的插件文件夹中创建文件。 表单如下所示:

<form style="margin-top:50px" action="#" method="post"> 
<input type="submit" name="a_submit" value="CSV laden">
</form>

我叫它的方式:

if(isset($_POST['a_submit'])){
            markenbuero_csv();
        }

我不确定我错过了什么。感谢您帮助我。

【问题讨论】:

  • “但不在我的插件文件夹中创建文件。” - 这可能是因为您只指定了文件名。此代码可能位于您的插件文件中,但这并不意味着它也是嵌入和运行您的插件代码的主脚本实例的工作目录stackoverflow.com/questions/20780422/…

标签: php wordpress csv


【解决方案1】:

使用绝对路径确保文件在正确的位置

$path = realpath('./your-directory') . DIRECTORY_SEPARATOR . 'demosaved.csv';
$file = fopen($path, 'w');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-12
    • 2011-03-18
    • 2021-09-05
    • 2013-03-08
    • 1970-01-01
    • 2013-10-18
    • 2017-08-31
    • 1970-01-01
    相关资源
    最近更新 更多