首页 > 解决方案 > file_put_contents 工作完美,直到从小部件移动到插件

问题描述

我成功构建了一个 WordPress 小部件,当刷新页面时,它会通过在 JSON 文件中找到的 URL 下载 .csv 文件。该小部件完美运行;每次刷新页面时,都会从 JSON 中的 URL 下载 .csv 文件。

现在,我从小部件中获取代码并将其粘贴到一个插件中,该插件每天通过 cron 作业执行相同的操作。不幸的是,当 cron 作业触发时,完全相同的代码现在不起作用。

虽然由于安全原因我无法发布所有代码,但这里是我遇到错误的摘录。

/* ISOLATE CSV LINK PULLED FROM JSON */
            $phpObj = json_decode($response);
            if (json_last_error() !== 0) {
                echo json_last_error_msg();
            } else {
                print_r($phpObj, true);
                $csvURL = $phpObj->result->location;
            }

            /* DOWNLOAD CSV FILE */

            $file_name = "filteredList" . $listLabelArray[$i] . ".csv";

            $info = pathinfo($file_name);

            if ($info["extension"] == "csv") {
                if(file_put_contents( "wp-content/plugins/finder-autoupdate/" . $file_name,
                    file_get_contents($csvURL))) {
                    echo nl2br($file_name ." downloaded successfully!\n");
                }
                else {
                    echo "File download failed.";
                }
            }
            else echo "Sorry, that's not a csv file";

这是调试控制台:

10-Jun-2021 16:59:39 UTC] PHP Warning:  file_put_contents(wp-content/plugins/finder-autoupdate /filteredListVA0002019921.csv):
failed to open stream: No such file or directory in /sites/[REDACTED].com/files/wp-
content/plugins/finder-autoupdate/finder-autoupdate.php on line 135

调查后,调试控制台中列出的路径不应该包含 finder-autoupdate.php,因为那是插件的 php 文件?路径应该只是放在filteredListVA0002019921.csv文件夹中/sites/[REDACTED].com/files/wp-content/plugins/finder-autoupdate/

标签: phpjsonwordpress

解决方案


通过插件“Advanced Cron Manager”手动运行 cron 作业会导致故障。当 cron 作业自动运行时,没有问题。解决了!


推荐阅读