php - file_put_contents worked perfectly until moved from widget to plugin

I successfully built a WordPress widget that, when a page is refreshed, downloads .csv files via their URLs found in a JSON file. The widget works perfectly; each time the page is refreshed, the .csv files are downloaded from their URLs in the JSON.
Now I am taking the code from the widget and pasting it into a plugin that does the same thing each day via a cron job. Unfortunately, the exact same code now doesn't work when triggered by the cron job.
While I can't post all the code due to security reasons, here's the excerpt where I'm encountering the bug.
/* 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";
Here's the debug console:
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
After looking into this, shouldn't the path listed in the debug console NOT include finder-autoupdate.php, as that's the plugin's php file? The path should instead just placefilteredListVA0002019921.csv
in the folder/sites/[REDACTED].com/files/wp-content/plugins/finder-autoupdate/
Answer
Solution:
Running the cron job manually via the plugin "Advanced Cron Manager" caused the glitch. When the cron job runs automatically, there's no issue. Solved!
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function format() on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.