-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.php
119 lines (108 loc) · 3.01 KB
/
File.php
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/*
@MikeRzhevsky [email protected]
*/
namespace App\Models;
use App\Db;
class File
{
const TABLE= 'fileStructure';
const CREATETABLE=' CREATE TABLE IF NOT EXISTS fileStructure (
id INT NOT NULL AUTO_INCREMENT,
filename varchar(200) NOT NULL,
filesize varchar(5) ,
filetype varchar(5) ,
moddate Date ,
PRIMARY KEY (id)
) ;';
public $id;
public $fileName;
public $filesize;
public $filetype;
public $moddate;
public static function prepareData()
{
$db = new Db();
$db->execute(self::CREATETABLE,self::class);
$existsREcords = $db->query(
'SELECT * FROM ' . self::TABLE,
[],
self::class);
if(empty($existsREcords)){
self::getfiles();
}
}
public static function deleteData()
{
$db = new Db();
return $db->query(
'DELETE FROM ' . self::TABLE,
[],
self::class);
}
public static function getfiles()
{
if( ( $dh = opendir($_SERVER['DOCUMENT_ROOT']) ) !== null ) {
$dataArray = array();
$ind = 0;
while (($file = readdir($dh)) !== false) {
$filename = $file;
$filesize = (string)filesize($file);
if (!$filesize)
$filesize = '0';
$filetype = pathinfo($file, PATHINFO_EXTENSION);
if (!$filetype)
$filetype = 'DIR';
$moddate = date("Y-m-d", filemtime($file));//Date('Y-m-d H:i:s')
$arr = array('filename' => $filename, 'filesize' => $filesize,
'filetype' => $filetype, 'moddate' => $moddate);
$ind++;
$file = new File();
$file->fileName = $filename;
$file->filetype = $filetype;
$file->filesize = $filesize;
$file->moddate = $moddate ;
$file->insert();
}
}
}
public static function findAll()
{
$db = new Db();
return $db->query(
'SELECT * FROM ' . self::TABLE,
[],
self::class);
}
public static function findById($id)
{
$db = new Db();
$sql = 'SELECT * FROM ' . self::TABLE . ' WHERE id:=id';
$data = $db->query(
$sql,
[],
self::class);
return $data ? $data[0] : null;
}
public function insert()
{
$fields = get_object_vars($this);
$cols = [];
$data = [];
foreach($fields as $name => $value)
{
if($name=='id'){
continue;
}
$cols[]=$name;
$data[':' . $name]= $value;
}
$sql = 'INSERT INTO ' .self::TABLE. '
(' .implode(',',$cols) .')
VALUES
(' .implode(',',array_keys($data)) . ')';
$db= new Db();
$db->execute($sql,$data);
$db->getLastId();
}
}