Skip to content

Commit b1b013c

Browse files
committed
Add $delete to optionally keep the tmp file after destruction
1 parent dfdb0d1 commit b1b013c

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ A convenience class for temporary files.
1313
## Features
1414

1515
* Create temporary file with arbitrary content
16-
* Delete file after use
16+
* Delete file after use (can be disabled)
1717
* Send file to client, either inline or with save dialog
1818
* Save file locally
1919

@@ -36,3 +36,12 @@ echo $file->getFileName();
3636
echo $file->getTempDir();
3737
```
3838

39+
If you want to keep the temporary file, e.g. for debugging, you can set the `$delete` property to false:
40+
41+
```php
42+
<?php
43+
use mikehaertl\tmp\File;
44+
45+
$file = new File('some content', '.html');
46+
$file->delete = false;
47+
```

src/File.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77
* A convenience class for temporary files.
88
*
99
* @author Michael Härtl <haertl.mike@gmail.com>
10-
* @version 1.0.1-dev
10+
* @version 1.1.0
1111
* @license http://www.opensource.org/licenses/MIT
1212
*/
1313
class File
1414
{
15+
/**
16+
* @var bool whether to delete the tmp file when it's no longer referenced or when the request ends.
17+
* Default is `true`.
18+
*/
19+
public $delete = true;
20+
1521
/**
1622
* @var string the name of this file
1723
*/
@@ -45,11 +51,13 @@ public function __construct($content, $suffix = null, $prefix = null, $directory
4551
}
4652

4753
/**
48-
* Delete tmp file on shutdown
54+
* Delete tmp file on shutdown if `$delete` is `true`
4955
*/
5056
public function __destruct()
5157
{
52-
unlink($this->_fileName);
58+
if ($this->delete) {
59+
unlink($this->_fileName);
60+
}
5361
}
5462

5563
/**

tests/FileTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,32 @@ public function testCanSaveFileAs()
5555
$tmp = new File($content);
5656
$fileName = $tmp->getFileName();
5757

58+
$this->assertFileExists($fileName);
5859
$this->assertTrue($tmp->saveAs($out));
5960
$this->assertFileExists($out);
6061
$readContent = file_get_contents($out);
6162
$this->assertEquals($content, $readContent);
6263
unset($tmp);
6364
$this->assertFileNotExists($fileName);
6465
$this->assertFileExists($out);
66+
unlink($out);
67+
}
68+
69+
public function testCanKeepTempFile()
70+
{
71+
$out = __DIR__.'/test.txt';
72+
$content = 'test content';
73+
$tmp = new File($content);
74+
$tmp->delete = false;
75+
$fileName = $tmp->getFileName();
76+
77+
$this->assertFileExists($fileName);
78+
$this->assertTrue($tmp->saveAs($out));
79+
$this->assertFileExists($out);
80+
unset($tmp);
81+
$this->assertFileExists($fileName);
82+
$this->assertFileExists($out);
83+
unlink($out);
6584
}
6685

6786
public function testCanCastToFileName()

tests/test.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)