简体   繁体   English

Cron作业 - 删除特定文件夹中的所有文件

[英]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 我想用php删除名为“data”的文件夹中的所有文件,并使用Cron Job

Cron Job is set to run script every hour, but i'm lost what should i write in the empty Cron Job设置为每小时运行一次脚本,但我失去了我应该写在空的内容

textfield and and how delete all files inside a specific folder in php?? textfield以及如何删除php中特定文件夹内的所有文件?

please someone explain me and help me out... 请有人解释我并帮助我...

在此输入图像描述

Fixed it: 修复:

Placed delete.php inside the empty field 将delete.php放在空字段内

and wrote inside delete.php the code down below: 并在delete.php里面写下面的代码:

<?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.';


?>

in cpanel go to cron job if you want to delete the folder including its contents: 如果要删除包含其内容的文件夹,请在cpanel中转到cron作业:

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/*

Here's a function from PHP that can remove a file. 这是PHP的一个可以删除文件的函数。

http://se2.php.net/unlink http://se2.php.net/unlink

And also, the example here; 而且,这里的例子; http://se2.php.net/manual/en/function.unlink.php#108940 http://se2.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) 包含有关如何从目录中删除文件的信息(只需跳过底部的rmdir)

Edit: Forgot about the cron-thing. 编辑:忘了关于cron-thing。 :) :)

If you create a file within your /home/a1206305/ called directory.php with this content: 如果您使用以下内容在/ home / a1206305 / called directory.php中创建文件:

<?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 然后在cron的第二个字段中,只需在directory.php中写入

只需提供您的php文件的完整文件路径(可以包括域名以及我们在浏览器中运行页面),然后在该页面中编写代码以删除任何文件夹中的所有文件。

enter full path of your php file and write code to delete all files from the respective directory. 输入php文件的完整路径并编写代码以删除相应目录中的所有文件。 php file code: php文件代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM