Coder Social home page Coder Social logo

laravel-cleantempfiles's Introduction

Laravel-CleanTempFiles

Delete temporal files with Laravel Command

Create command from console:

php artisan command:make cleanTmp

This will create the file app/commands/cleanTmp.php

On fire function paste:

	public function fire()
	{
		$this->info('Deleting all temp files');

		//Clean before X hours
		$saveHours = 2;

		//Get timestamp - $saveHours
		$hour_limit = new DateTime('now');
		$hour_limit->modify('-'.$saveHours.' hour');
		$timestamp_limit = $hour_limit->getTimestamp();
		$this->info('hour: '.$timestamp_limit);

		//Get all files - recursive
		$files = File::allFiles( Config::get('settings.system.tmp_path') );
		//Foreach all files from temp folder
		foreach ($files as $file){
			try {
				//If modified is < $saveHours
				if(File::lastModified($file) < $timestamp_limit){
					$this->info('4 Delete' . $this->info((string)$file));
					File::delete($file);
				}
			} catch (Exception $e){
				//Log error
				$msg = 'Delete error' . (string)$file;
				$this->error($msg);

				//Notify to developer contact
				Mail::send(array('text' => 'emails.log'), array('msg'=>$msg), function($message){
					$message->to( Config::get('settings.contacts.developer') )->subject( URL::to('/') . ' - Log error - cleanTmp command');
				});
			}
		}
	}

On app/start/artisan.php add:

Artisan::add(new cleanTmp);

Note that two configuration items are used:

//Email log
Config::get('settings.contacts.developer')
//Tmp path
Config::get('settings.system.tmp_path')
  • The deletion of files is recursive.

###Cronjob###

sudo crontab -e

And add the following line to run the task once daily at 00h

00 00 * * * php /var/www/project/artisan command:cleanTmp

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.