Hi, I was just wondering where i have made a mistake with my code. I am trying to save a file with the current date to a folder, than delete all files in that folder that are over 64days old. Currently I get these errors.
PHP Error Message
Warning: file_get_contents(pathtoaexternalfile?format=xml) [function.file-get-contents]: failed to open stream: No such file or directory in /home/a5403572/public_html/archive/cron.php on line 14
Warning: file_put_contents(/archive/06.10.12.rss) [function.file-put-contents]: failed to open stream: No such file or directory in /home/a5403572/public_html/archive/cron.php on line 18
Warning: opendir(/archive/) [function.opendir]: failed to open dir: No such file or directory in /home/a5403572/public_html/archive/cron.php on line 26
Here is my Code.
<?php
//save current
$today = date("m.d.y");
$file = 'pathtoaexternalfile?format=xml';
// Open the file to get existing content
$current = file_get_contents($file);
// Write the contents back to the file
file_put_contents('/archive/'.$today.'.rss', $current);
// delete all files over x days
$days= 64 ;
$dir = '/archive/';
if ($handle = opendir($dir)) {
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
if ($file[0] == '.' || is_dir("$dir/$file")) {
// ignore hidden files and directories
continue;
}
if ((time() - filemtime($file)) > ($days *86400)) { //7 days
unlink("$dir/$file");
}
}
closedir($handle);
}
?>
Note: the file is called cron.php, Also the pathtoaexternalfile?format=xml is not actually pathtoaexternalfile?format=xml it is in the format http://www.website.com/dir/file?format=xml
Thanks in advance
Update:Update: thanks for your reply, but what do i do if it is an external file
Copyright © 2024 VQUIX.COM - All rights reserved.
Answers & Comments
Verified answer
The error on line 14 indicates that PHP is unable to access the external resource specified. Since your code depends on this resource you may want to handle the error in a meaningful way since external resources can be unreliable. Perhaps terminate the script and send an email to alert you of the problem. If you continue to have a problem with fopen() you can try something more robust like cURL.
Regarding the error on line 26, PHP is trying to access /archive/ which is at the root level of the file hierarchy. It says this directory doesn't exist. Do you mean to open /home/a5403572/public_html/archive/ instead? If so, putting a dot in from of /archive/ might help: $dir = './archive/';
No, you do not could desire to 'team with the aid of' while utilising MIN(). i'm slightly at a loss for words with your database schema. it type of feels as in case you have a database named 'jokes', then you definately are doing stuff on 'db' with a table named additionally 'jokes'. are you able to make sure what your database and table names are? additionally, if the above queries have been changed, the blunders message which you exact is often led to with the aid of utilising a field call this is a reserved notice (i.e.: it has a distinctive meaning). To be secure, consistently enclose your field names interior the backquotes (the character next to the quantity 'a million' on your keyboard). e.g.: pick `question`, `answer`, `identity`, `perspectives` FROM ...
pathtoaexternalfile?format=xml
This is not path to file of course
------------------
you need to specify a file path from the way
/home/a5403572/public_html/archive/