PHP code example of ctw / ctw-temp

1. Go to this page and download the library: Download ctw/ctw-temp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ctw / ctw-temp example snippets


define('APP_PATH_TEMP', sprintf(
    '%s/%s_%s_temp',
    sys_get_temp_dir(),
    'www.example.com',
    hash('crc32b', 'www-data_www-data'),
));
mkdir(APP_PATH_TEMP, 0777, true);

use Ctw\Temp\Temp;

$temp = new Temp('www.example.com');     // base defaults to /var/tmp/php
define('APP_PATH_TEMP', $temp->createPath());

use Ctw\Temp\Temp;

// Full constructor signature (only $id is -cache',   // optional extra directory
    e.com/page-cache'

$temp->createPath();    // mkdir -p; throws RuntimeException if not writable
$temp->existsPath();    // bool

// Create and later delete a uniquely named file inside the directory:
$file = $temp->createFile('report', 'pdf'); // '/…/report-a1b2c3d4e5f6a7b8.pdf'
$temp->deleteFile($file);

$temp->clearPath();     // empty the directory, keep it (e.g. cache reset)
$temp->trashPath();     // same, in constant time; returns '/var/tmp/php/<hash>/.trash-9f3a1c2b7d4e5061'
$temp->deletePath();    // recursively remove the directory and its contents

$temp = new Temp('www.example.com', ['page-cache', 'v2']);
$temp->getPath(); // '/var/tmp/php/78b43994/www.example.com/page-cache/v2'

// bootstrap.php
define('APP_PATH_TEMP', new Temp(hostname_www())->createPath());

// invalidate.php
$temp = Temp::createFromPath(APP_PATH_TEMP);
$temp->trashPath();

$temp = new Temp('www.example.com');

Temp::createFromPath($temp->getPath()) == $temp;  // true

$temp = Temp::createFromPath('/srv/tmp/www.example.com/page-cache', '/srv/tmp');

use Ctw\Temp\Temp;
use Ctw\Temp\TempTrait;

final class PageCache
{
    use TempTrait;

    public function invalidate(): void
    {
        $this->getTemp()->trashPath();
    }
}

$cache = new PageCache()->setTemp(Temp::createFromPath(APP_PATH_TEMP));
cron
17 4 * * * /root/bin/clear_trash.php
cron
17 4 * * * /root/bin/clear_trash.php --base-path=/srv/tmp