user1480092
user1480092

Reputation: 387

Cron Job - delete all files inside a specific folder

I want to delete all files inside a folder called " data " with php and using Cron Job, the

Cron Job is set to run script every hour, but i'm lost what should i write in the empty

textfield and and how delete all files inside a specific folder in php??

please someone explain me and help me out...

enter image description here

Fixed it:

Placed delete.php inside the empty field

and wrote inside delete.php the code down below:

<?php


define('PATH', 'folder/');

function destroy($dir) {
    $mydir = opendir($dir);
    while(false !== ($file = readdir($mydir))) {
        if($file != "." && $file != "..") {
            chmod($dir.$file, 0777);
            if(is_dir($dir.$file)) {
                chdir('.');
                destroy($dir.$file.'/');
                rmdir($dir.$file) or DIE("couldn't delete $dir$file<br />");
            }
            else
                unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
        }
    }
    closedir($mydir);
}
destroy(PATH);
echo 'all done.';


?>

Upvotes: 5

Views: 20883

Answers (4)

Shuhad zaman
Shuhad zaman

Reputation: 3390

in cpanel go to cron job if you want to delete the folder including its contents:

rm -rf /home/user/public_html/folder

if you want to remove everything in this folder, but leave the folder itself:

rm -f /home/user/public_html/folder/*

Upvotes: 12

NoLifeKing
NoLifeKing

Reputation: 1939

Here's a function from PHP that can remove a file.

https://www.php.net/unlink

And also, the example here; https://www.php.net/manual/en/function.unlink.php#108940

Contains information on how you can delete files from a directory (just skip the rmdir at the bottom)

Edit: Forgot about the cron-thing. :)

If you create a file within your /home/a1206305/ called directory.php with this content:

<?php
    $path = "/home/a1206305/domain.com/data/";
    foreach(glob($path . "*") as $file)
    {
        if(is_file($path . $file))
            unlink($path . $file);
    }
?>

And then in that second field for cron, just write in directory.php

Upvotes: 4

Anil
Anil

Reputation: 11

enter full path of your php file and write code to delete all files from the respective directory. php file code:

$dir = 'your directory path';
foreach(glob($dir.'*.*') as $v){
unlink($v);
}

Upvotes: 1

Ravinder Singh
Ravinder Singh

Reputation: 3133

Just give the full file path of your php file(can include domain name as well like we run the page in browser) there and in that page write the code to delete all the files inside any folder.

Upvotes: 1

Related Questions