php - 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/
解决方案
通过插件“Advanced Cron Manager”手动运行 cron 作业会导致故障。当 cron 作业自动运行时,没有问题。解决了!
推荐阅读
- ssl - 如何使用 `electron.net` API 信任来自主进程的电子应用程序的自签名证书?
- reactjs - 如何在reactjs中对获取的数据表进行排序
- excel - startRange.offset - 对象不支持此属性或方法
- c# - C# 通用列表
更新项目 - spring-boot - 第三方 API 的执行器健康检查
- android - 单击警报对话框上的退出按钮会显示 MainActivity
- javascript - 当另一个用户在同一服务器上打开另一个网站时,Socket.io 监听发出消息
- java - 生成文档的 Java 注释的哪个生命周期?
- sql - SQL如何删除动态命名的外键约束
- ios - UIAlert 和视图控制器的错误