Php 如何在1周后从laravel 6中的文件夹和数据库中删除文件?

Php 如何在1周后从laravel 6中的文件夹和数据库中删除文件?,php,laravel,Php,Laravel,目前,我将这些文件存储在一个名为“文件”的公共文件夹中 我希望它在1周后自动删除该文件,并将其从数据库中删除 FileController.php public function store(Request $request) { $file = $request->file('file'); $new_name = rand() . '.' . $file->getClientOriginalExtension(); $fil

目前,我将这些文件存储在一个名为“文件”的公共文件夹中

我希望它在1周后自动删除该文件,并将其从数据库中删除

FileController.php

public function store(Request $request)
    {
        $file = $request->file('file');
        $new_name = rand() . '.' . $file->getClientOriginalExtension();
        $file->move(public_path("files"), $new_name);

        $upload = new File();
        $upload->ip = $request->ip();
        $upload->file_name = $new_name;
        $upload->save();

        return redirect('/file');
    }
protected function schedule(Schedule $schedule)
{
    // $schedule->command('inspire')
    //          ->hourly();

    $schedule->call(function () {
        DB::table('files')->whereRaw('created_at >= now() - interval 168 hour')->delete();
    })->weekly();
}
Kernal.php

public function store(Request $request)
    {
        $file = $request->file('file');
        $new_name = rand() . '.' . $file->getClientOriginalExtension();
        $file->move(public_path("files"), $new_name);

        $upload = new File();
        $upload->ip = $request->ip();
        $upload->file_name = $new_name;
        $upload->save();

        return redirect('/file');
    }
protected function schedule(Schedule $schedule)
{
    // $schedule->command('inspire')
    //          ->hourly();

    $schedule->call(function () {
        DB::table('files')->whereRaw('created_at >= now() - interval 168 hour')->delete();
    })->weekly();
}

假设
files
表中有一个
path
字段,该字段的路径位于该文件的
public
内,则只需编辑该函数即可获得要删除的文件,然后删除该文件记录:

$queryBuilder=DB::table('files')->whereRaw('created_at>=now()-interval 168 hour');
foreach($queryBuilder->get()作为$file){
文件::删除(public_path().$File->path);
}
$queryBuilder->delete()

将“自动删除”列添加到表中,在此列中,您必须存储上载文件将被删除的日期

FileController.php

use Carbon;
public function store(Request $request)
{
    $upload = new File();
    ...
    ...
    $days_after_file_delete = 7;
    $upload->automatically_delete_at = Carbon::now()->addDays($days_after_file_delete)->format('Y-m-d');
    $upload->save();
    ...
}


Kernal.php

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
      $files = DB::table('files')->whereDate('automatically_delete_at', Carbon::now()->format('Y-m-d'))->get();
      here you get all files which should be delete
      ...
      your code
      ... 
    })->daily();
}

我将创建commmand,您将从数据库名称中获得日期create,如果您有时间戳,您可以使用它,如果没有,您应该创建类似createDate的列。 看看这个:

 $time=date('Y-m-d', strtotime(date("Y-m-d") . " -7 day")); //this will give you date from last week
 $names=DB::table('files')->select('file_name')->whereRAW("date(createDate)='$time'")->get()->each(function ($file){
        unlink(public_path('files\\'.$file->file_name)) //this delete from folder
        DB::table('files')->where('file_name',$file->file_name)->delete(); //this delete from database
    });
最后,您应该添加内核调度器,它将每天运行


就这些

文件路径存储在哪里?公用文件夹中的“文件”文件夹在哪里