-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.php
executable file
·78 lines (63 loc) · 2.54 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
<?php
namespace hrupin\file;
/**
* This is just an example.
*/
class File extends \yii\base\Widget
{
// $typeRead
//
//r – открытие файла только для чтения.
//r+ - открытие файла одновременно на чтение и запись.
//w – создание нового пустого файла. Если на момент вызова уже существует такой файл, то он уничтожается.
//w+ - аналогичен r+, только если на момент вызова фай такой существует, его содержимое удаляется.
//a – открывает существующий файл в режиме записи, при этом указатель сдвигается на последний байт файла (на конец файла).
//a+ - открывает файл в режиме чтения и записи при этом указатель сдвигается на последний байт файла (на конец файла). Содержимое файла не удаляется.
protected $errors = [];
protected $file;
protected $path;
public function __construct($dir, $file, $typeRead = 'r+'){
if($this->assetFile($dir, $file)){
$this->openFile($typeRead);
}
}
public function createFile($dir, $file){
$this->path = $dir.'/'.$file;
$this->file = fopen($this->path, "w");
}
private function setErrors($key, $err){
$this->errors[$key] = $err;
}
public function getErrors(){
return $this->errors;
}
private function openFile($t){
$this->file = fopen($this->path, $t);
}
public function writeFile($text){
return fwrite($this->file, $text);
}
public function readAllFile(){
return readfile($this->path);
}
public function readStrFile($str){
return fgets($this->file, $str);
}
public function closeFile(){
return fclose($this->file);
}
private function assetFile($dir, $file){
if(file_exists($dir)){
if(file_exists($dir.'/'.$file)){
$this->path = $dir.'/'.$file;
return true;
}
else{
$this->setErrors('file', 'Файла в этом каталоге не существует');
}
}
else{
$this->setErrors('dir', 'Каталога не существует');
}
}
}