├── LICENSE ├── README.md ├── composer.json └── src └── Makasim └── File └── TempFile.php /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013 by makasim 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TempFile 2 | ======== 3 | 4 | TempFile extends SplFileInfo class. It represents temporary file which will be removed at script shutdown. 5 | 6 | Examples: 7 | 8 | ```php 9 | persist(); 21 | ``` 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "makasim/temp-file", 3 | "type": "library", 4 | "description": "Temp file object", 5 | "keywords": ["file", "temp", "temp file"], 6 | "homepage": "https://github.com/makasim/TempFile", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Maksim Kotlyar", 11 | "email": "kotlyar.maksim@gmail.com" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.3.2" 16 | }, 17 | "autoload": { 18 | "psr-0": { 19 | "Makasim": "src/" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Makasim/File/TempFile.php: -------------------------------------------------------------------------------- 1 | 6 | * @since 8/15/12 7 | */ 8 | class TempFile extends \SplFileInfo 9 | { 10 | /** 11 | * @var array of string paths array(path => path) 12 | */ 13 | protected static $tempFiles = array(); 14 | 15 | /** 16 | * {@inheritdoc} 17 | */ 18 | public function __construct($fileName) 19 | { 20 | parent::__construct($fileName); 21 | 22 | self::registerRemoveTempFilesHandler(); 23 | self::$tempFiles[$fileName] = $fileName; 24 | } 25 | 26 | /** 27 | * Persist file so that it would not be removed at the end of the script execution. 28 | * 29 | * @return \SplFileInfo 30 | */ 31 | public function persist() 32 | { 33 | unset(self::$tempFiles[(string) $this]); 34 | 35 | return $this->getFileInfo(); 36 | } 37 | 38 | /** 39 | * Creates a temp file with unique filename. 40 | * 41 | * @param string $prefix 42 | * 43 | * @return TempFile 44 | */ 45 | public static function generate($prefix = 'php-tmp-file', $suffix = '') 46 | { 47 | $filename = tempnam(sys_get_temp_dir(), $prefix); 48 | 49 | if ($suffix) { 50 | $i = 0; 51 | do { 52 | $newFilename = $filename . $i++ . $suffix; 53 | } while (file_exists($newFilename)); 54 | 55 | rename($filename, $newFilename); 56 | $filename = $newFilename; 57 | } 58 | 59 | return new static($filename); 60 | } 61 | 62 | /** 63 | * Creates a temp file from an exist file keeping it safe. 64 | * 65 | * @param mixed $file 66 | * @param string $prefix 67 | * 68 | * @return TempFile 69 | */ 70 | public static function from($file, $prefix = 'php-tmp-file') 71 | { 72 | $tmpFile = static::generate($prefix); 73 | 74 | copy($file, $tmpFile); 75 | 76 | return $tmpFile; 77 | } 78 | 79 | private static function registerRemoveTempFilesHandler() 80 | { 81 | static $registered = false; 82 | if ($registered) { 83 | return; 84 | } 85 | 86 | $tempFiles = &self::$tempFiles; 87 | 88 | register_shutdown_function(function() use (&$tempFiles) { 89 | foreach ($tempFiles as $tempFile) { 90 | if (file_exists($tempFile)) { 91 | @unlink($tempFile); 92 | } 93 | } 94 | }); 95 | 96 | $registered = true; 97 | } 98 | } --------------------------------------------------------------------------------