I'm tring to use this code to delete all files older than x days in 2 folder but I'm getting an error:
PHP Parse error: syntax error, unexpected T_PUBLIC in E:\\home\\ca\\web\\cm\\cache.php on line 6
Why?
<?php
$pastas = array("gallery-images/","resources/cache/");
foreach($pastas as $pasta){
$this->deleteFrom($pasta);
}
public function deleteFrom($path){
$expiretime=10080; //expire time in minutes, 7 days = 7*24*60
$tmpFolder=$path.'/';
$fileTypes="*.*";
foreach (glob($tmpFolder . $fileTypes) as $Filename) {
// Read file creation time
$FileCreationTime = filectime($Filename);
// Calculate file age in seconds
$FileAge = time() - $FileCreationTime;
// Is the file older than the given time span?
if ($FileAge > ($expiretime * 0)){
// Now do something with the olders files...
echo "The file $Filename is older than $expiretime minutes\n";
//delete files:
unlink($Filename);
}
}
}
?>
Your function definition is out of a class so you need to remove PUBLIC
public function deleteFrom($path){
to
function deleteFrom($path){
Also $this->
is not correct you need to remove them and call the function as
deleteFrom($pasta);
Check here how Public, Private etc are used in PHP http://php.net/manual/en/language.oop5.visibility.php
this is because not using class here , so no need to use $this
to call your function just use deleteFrom()
please see the code below
foreach($pastas as $pasta){
deleteFrom($pasta);
}
If you want to use a class, you could write something like that :
$pastas = array("gallery-images/", "resources/cache/");
$File = new File();
foreach ($pastas as $pasta)
{
$File->deleteFrom($pasta);
}
class File
{
public function deleteFrom($path)
{
$expiretime=10080; //expire time in minutes, 7 days = 7*24*60
$tmpFolder=$path.'/';
$fileTypes="*.*";
foreach (glob($tmpFolder . $fileTypes) as $Filename)
{
// Read file creation time
$FileCreationTime = filectime($Filename);
// Calculate file age in seconds
$FileAge = time() - $FileCreationTime;
// Is the file older than the given time span?
if ($FileAge > ($expiretime * 0))
{
// Now do something with the olders files...
echo "The file $Filename is older than $expiretime minutes\n";
//delete files:
unlink($Filename);
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.