blob: f9c5b89e6050b0e1d7022900052b7a72e3024272 (
plain)
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
120
|
<?php
// model/Article.php
// éventuellement: utiliser une classe structure de données
// accesssible avec des GET et des SET
// à voir alors si on utilise des variables statiques
class Article
{
// pour tous les articles
public $page; // page et donc dossier concerné
public $format = 'html'; // vaut 'html' ou 'json'
public $fileListCount;
public $fileList; // = toutes les données
// pour un article (ou album) spécifique
//public $fileName = ''; // = $_SESSION['nomFichier']
public $fileCode = ''; // $_SESSION['target']
protected $time; // timestamp pour noms des fichiers créés
public function __construct($page)
{
$this->page = $page;
$this->time = time();
$this->makeFileList();
}
// tableaux des noms des fichiers
// noter que le chemin et l'extension ne varient pas
public function makeFileList()
{
// globbing = utiliser un pattern pour cibler des fichiers
$nameList = glob('data/' . $this->page . '/' . $this->format . '/*.' . $this->format);
$this->fileListCount = count($nameList);
for($i = 0; $i < $this->fileListCount; $i++)
{
$pathInfo = pathinfo($nameList[$i]);
$fileCode = $pathInfo['filename'];
$this->fileList[$i] = [
'fileCode' => $fileCode,
'fileName' => $nameList[$i],
'content' => '',
//'date' => getdate() // peut-être utile plus tard
];
}
//var_dump($this->fileList); die();
}
// GET
// SET
/*public function setFileName()
{
if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format))
{
$this->fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
}
}*/
// fonctions CRUD (create - read - update - delete)
// create
public function create($content)
{
//$format = 'html';
// nommer les fichiers avec le timestamp pour:
// - les trier par ordre chronologique
// - rendre quasi impossible d'avoir deux fois le même nom
$fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->time . '.' . $this->format;
$file = fopen($fileName, 'w'); // w pour créer ou écraser
fputs($file, $content);
fclose($file);
chmod($fileName, 0666);
}
// read
public function readAll() // ajoute le contenu à FileList
{
for($i = 0; $i < $this->fileListCount; $i++)
{
$this->fileList[$i]['content'] = file_get_contents($this->fileList[$i]['fileName']);
}
//var_dump($this->fileList); die();
}
public function readOne()
{
//return(file_get_contents($this->fileName));
return(file_get_contents('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format));
}
// update
public function update($content)
{
$fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
//var_dump(file_get_contents($fileName)); die();
if(file_exists($fileName) && empty($content))
{
$this->delete();
}
elseif(!empty($content))
{
$file = fopen($fileName, 'w'); // w pour créer ou écraser
fputs($file, $content);
fclose($file);
//chown($this->fileName, 'http');
chmod($fileName, 0666);
}
}
// delete
public function delete()
{
unlink('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format);
}
}
|