PHP code example of zenstruck / temp-file

1. Go to this page and download the library: Download zenstruck/temp-file 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/ */

    

zenstruck / temp-file example snippets


use Zenstruck\TempFile;

// create empty file with random name (in /tmp)
$file = TempFile::new();

// create empty file with random filename, with extension (in /tmp)
$file = TempFile::withExtension('txt');

// create file with specific filename (in /tmp)
$file = TempFile::withName('my-file.txt'); // creates empty file
$file = TempFile::withName('my-file.txt', 'some content'); // creates file with string content
$file = TempFile::withName('my-file.txt', \fopen('some/file.txt', 'r')); // creates file with resource as content
$file = TempFile::withName('my-file.txt', new \SplFileInfo('some/file.txt')); // creates file from existing file (existing file is copied)

// create for existing file
$file = TempFile::new('some/file.txt'); // note: will be deleted at the end of the script

// create with string content
$file = TempFile::for('some contents');

// create with resource
$file = TempFile::for(\fopen('some/file.txt', 'r'));

// create from another file (existing file is copied)
$file = TempFile::for(new \SplFileInfo('some/file.txt'));

// create image
$image = TempFile::image(); // temporary 10x10 image with 'jpg' as the extension
$image = TempFile::image(100, 50); // customize the dimensions
$image = TempFile::image(type: 'gif'); // customize the image type
$image = TempFile::image(name: 'my-image.png'); // customize the file name

/** @var \Zenstruck\TempFile $file */

$file->contents(); // string - the file's contents
$file->refresh(); // self - clearstatcache() on the file (refreshes metadata)
$file->delete(); // self - delete the file

// is instance of \SplFileInfo
$file->getMTime(); // int - last modified timestamp
$file->getExtension(); // string - file extension