【发布时间】:2021-06-10 17:17:36
【问题描述】:
我成功构建了一个 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/ 中
【问题讨论】:
-
您可以尝试使用 WP_CONTENT_DIR 而不是使用相对链接吗?如果在您移动它之前它仍然有效,我首先要检查的是链接现在是否断开。
-
@JosephOdom 感谢您的回复!事实证明,如果我使用插件“Advanced Cron Manager”手动触发 cron 作业,问题就会显现出来。今天早上我让 cron 作业自动运行,一切正常。