-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFile.php
More file actions
70 lines (63 loc) · 1.71 KB
/
File.php
File metadata and controls
70 lines (63 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php namespace Mreschke\Helpers;
/**
* File and folder helpers.
* @copyright 2014 Matthew Reschke
* @license http://mreschke.com/license/mit
* @author Matthew Reschke <mail@mreschke.com>
*/
class File
{
/**
* Write data to a file
* @param string $file full path to file
* @param string $data
* @return void
*/
public static function write($file, $data)
{
file_put_contents($file, $data);
}
/**
* Append $data to a file.
* @param string $file full path to file
* @param string $data to write
* @return void
*/
public static function append($file, $data)
{
$fp = fopen($file, 'a');
fwrite($fp, $data);
fclose($fp);
}
/**
* Returns a string of a new random filename.
* @param string $name Is prepended + _ if set
* @param string $path Is prepended if set, defaults to /tmp
* @param string $extension is appended if set, defaults to tmp (do not include leading .)
* @return string
*/
public static function getNewTmpFile($name = '', $path = '/tmp', $extension = 'tmp')
{
if ($name) {
$name .= '_';
}
if (!$extension) {
$extension = 'tmp';
}
if (substr($path, -1) != '/') {
$path .= "/";
}
return $path.$name.Str::getMd5().".tmp";
}
/**
* Get all open files in path (recursive), uses lsof, requres root privileges to get most lsof file results
* @param string $path
* @return array
*/
public static function getOpenFiles($path)
{
$cmd = "lsof -Fn | grep $path | sort -u | sed 's/^.//'";
exec($cmd, $files);
return $files;
}
}