Created
February 23, 2018 21:55
-
-
Save Sentinel17/a1fd9f8d818844cdf54c497a90fa2996 to your computer and use it in GitHub Desktop.
Домашнее задание №11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'Database.php'; | |
class csvConvert extends Database{ | |
public function getArrFromCsv($file){ | |
$fp = fopen($file, 'r'); | |
$arr = []; | |
while ($line = fgetcsv($fp, filesize($file), ";")) { | |
$arr[] = $line; | |
} | |
fclose($fp); | |
return $arr; | |
} | |
public function getCsvFromArr($arr, $name){ | |
$fp = fopen($name.'.csv', 'a+'); | |
foreach ($arr as $value) { | |
fwrite($fp,'"' .implode($value, '";"').'"' . PHP_EOL); | |
} | |
fclose($fp); | |
} | |
public function csvToDb($file){ | |
$fp = fopen($file, 'r'); | |
while (!feof($fp)) { | |
$param = []; | |
$str = fgetcsv($fp, 10000, ';'); | |
foreach ($str as $value) { | |
$param[] = $value; | |
} | |
$stm = $this->data->prepare('INSERT INTO users SET login = ?, password = ?, fio = ?, adress = ?,'); | |
$stm->execute($param); | |
} | |
fclose($fp); | |
} | |
public function DbToCsv($filename){ | |
$fp = fopen($filename.'.csv', 'a+'); | |
$stmt = $this->data->query('SELECT * FROM users'); | |
while ($row = $stmt->fetch()) { | |
fwrite($fp,'"' .implode($row, '";"').'"' . PHP_EOL); | |
} | |
fclose($fp); | |
} | |
} | |
$convert = new csvConvert(); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Database{ | |
public $isConn; | |
protected $data; | |
public function __construct($username = "root", $password = "", $host = "127.0.0.1", $dbname = "test", $options = []){ | |
$this->isConn = TRUE; | |
$this->data = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password, $options); | |
$this->data->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$this->data->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
} | |
public function Disconnect(){ | |
$this->data = NULL; | |
$this->isConn = FALSE; | |
} | |
public function getRow($params = []){ | |
$stmt = $this->data->prepare("SELECT * FROM users WHERE id = ?"); | |
$stmt->execute($params); | |
return $stmt->fetch(); | |
} | |
} | |
$user = new Database(); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'csv.php'; | |
$arr = [ | |
['awdawd', 'wfwa', 'awfwa'], | |
['w33w3', 'w34gw3', 'gw43g'], | |
['awfw', 'asf', 'asfsf'] | |
]; | |
$convert->getCsvFromArr($arr, 'test'); | |
$arr = $convert->getArrFromCsv('test.csv'); | |
var_dump($arr); | |
$convert->DbToCsv('test1'); | |
$convert->csvToDb('test1.csv'); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Работа с файлами --> | |
<!-- Создайте файл 'test.txt' и запишите в него фразу 'Привет, мир!'. --> | |
<?php | |
$h1 = fopen('test.txt', 'w'); | |
fwrite($h1, 'Привет, мир!'); | |
fclose($h1); | |
?> | |
<!-- Считайте данные из файла 'test.txt' и выведите их на экран. --> | |
<?php | |
$h1 = fopen('test.txt', 'r'); | |
$text = fread($h1, filesize('test.txt')); | |
echo $text; | |
fclose($h1); | |
?> | |
<!-- Переименуйте файл 'test.txt' в 'mir.txt'. --> | |
<?php | |
echo rename('test.txt', 'mir.txt'); | |
?> | |
<!-- Создайте копию файла 'mir.txt' и назовите ее 'world.txt'. --> | |
<?php | |
copy('mir.txt', 'world.txt'); | |
?> | |
<!-- Определите размер файла 'world.txt'. Выведите его на экран. Выведите его в байтах, мегабайтах, гигабайтах. --> | |
<?php | |
echo filesize('world.txt'); //байт | |
echo filesize('world.txt')/1024/1024; //мегабайт | |
echo filesize('world.txt')/1024/1024/1024; //гигабайт | |
?> | |
<!-- Удалите файл 'world.txt'. --> | |
<?php | |
unlink('world.txt'); | |
?> | |
<!-- Проверьте существование файлов 'world.txt' и 'mir.txt'. --> | |
<?php | |
if (file_exists('world.txt')) { | |
echo "Файл существует"; | |
} else { | |
echo "Файл не существует"; | |
} | |
?> | |
<!-- Работа с папками, mkdir, rmdir --> | |
<!-- Создайте папку 'test'. --> | |
<?php | |
mkdir('test'); | |
?> | |
<!-- Переименуйте папку 'test' на 'www'. --> | |
<?php | |
rename('test','www'); | |
?> | |
<!-- Удалите папку 'www'. --> | |
<?php | |
rmdir ('www'); | |
?> | |
<!-- Дан массив со строками. Создайте в папке 'test' папки, названиями которых служат элементы этого массива. --> | |
<?php | |
mkdir('test'); | |
$arr = ['css','js','php']; | |
foreach($arr as $value) { | |
mkdir('test/'.$value); | |
} | |
?> | |
<!-- На scandir, is_dir, is_file, PHP_EOL --> | |
<!-- Выведите на экран название всех файлов и подпапок из папки 'test'. --> | |
<?php | |
var_dump(scandir('test')); | |
?> | |
<!-- Выведите на экран название всех файлов, но не подпапок из папки 'test'. --> | |
<?php | |
$arr = []; | |
$dir = scandir('test'); | |
foreach($dir as $value){ | |
if(is_file('test/'.$value)){ | |
$arr[] = $value; | |
} | |
} | |
var_dump($arr); | |
?> | |
<!-- В папке 'test' есть файлы и подпапки. Выведите на экран содержимое всех файлов, которые лежат непосредственно в папке 'test'. --> | |
<?php | |
$arr = []; | |
$dir = scandir('test'); | |
foreach($dir as $value){ | |
if(is_file('test/'.$value)){ | |
$arr[] = $value; | |
} | |
} | |
var_dump($arr); | |
?> | |
<!-- Выведите на экран название всех файлов с расширением txt из папки 'test'. --> | |
<?php | |
$arr = []; | |
$dir = scandir('test'); | |
foreach($dir as $value){ | |
if(is_file('test/'.$value) && preg_match('#.txt$#',$value)){ | |
$arr []= $value; | |
} | |
} | |
var_dump($arr); | |
?> | |
<!-- Найдите все файлы из папки 'test' и вставьте в начало каждого файла полный путь к нему (текст файла должен остаться в нем и начинаться с новой строки после пути). --> | |
<?php | |
$arr = []; | |
$dir = scandir('test'); | |
foreach($dir as $value){ | |
if(is_file('test/'.$value)){ | |
$text = file_get_contents('test/'.$value); | |
file_put_contents('test/'.$value, 'test/'.$value.PHP_EOL.$text); | |
} | |
} | |
?> | |
<!-- Выведите на экран имена всех папок из папки 'test' и их подпапок (может быть любой уровень вложенности). --> | |
<?php | |
$arr = []; | |
function show_dir($dir_name){ | |
$dir = scandir($dir_name); | |
unset( $dir[0], $dir[1] ); | |
foreach($dir as $value){ | |
$str = $dir_name; | |
if (is_dir($dir_name.'/'.$value)){ | |
$str .= '/'.$value; | |
$arr[$value] = show_dir($str); | |
} | |
} | |
return $arr; | |
} | |
var_dump(show_dir('test')); | |
?> | |
<!-- Выведите на экран содержимое всех файлов из папки 'test' и ее подпапок (может быть любой уровень вложенности). --> | |
<?php | |
$arr = []; | |
function show_dir($dir_name){ | |
$dir = scandir($dir_name); | |
unset($dir[0], $dir[1]); | |
foreach($dir as $value) { | |
$str = $dir_name; | |
if(is_file($dir_name.'/'.$value)) { | |
$arr[$value] = file_get_contents($dir_name.'/'.$value); | |
} | |
if(is_dir($dir_name.'/'.$value)) { | |
$str .= '/'.$value; | |
$arr[$value] = show_dir($str); | |
} | |
} | |
return $arr; | |
} | |
var_dump(show_dir('test')); | |
?> | |
<!-- Найдите все файлы из папки 'test' и ее подпапок любого уровня вложенности и вставьте в начало каждого файла полный путь к нему (текст файла должен остаться в нем и начинаться с новой строки после пути). --> | |
<?php | |
function insertFullPath($file){ | |
$dir = scandir($file); | |
unset($dir[0], $dir[1]); | |
foreach($dir as $key) { | |
if (is_dir($file.'/'.$key)) { | |
insertFullPath($file.'/'.$key); | |
} else { | |
$textf = file_get_contents($file.'/'.$key); | |
$textf = $file.'/'.$key.PHP_EOL.$textf; | |
file_put_contents($file.'/'.$key, $textf); | |
} | |
} | |
} | |
insertFullPath('test'); | |
?> | |
<!-- Задачи --> | |
<!-- Удалите из папки 'test' все файлы размером более 1мб. --> | |
<?php | |
$dir = scandir('test'); | |
foreach($dir as $value){ | |
if(is_file('test/'.$value) && filesize('test/'.$value)/1024/1024 > 1){ | |
unlink('test/'.$value); | |
} | |
} | |
?> | |
<!-- Имеется папка с файлами, узнайте размер этой папки. --> | |
<?php | |
$dir = scandir('test'); | |
$size = 0; | |
foreach($dir as $value){ | |
if(is_file('test/'.$value)){ | |
$size += round(filesize('test/'.$value)/1024/1024); | |
} | |
} | |
echo $size.' мб'; | |
?> | |
<!-- Имеется папка с подпапками, узнайте размеры всех подпапок папки и выведите их на экран. --> | |
<?php | |
$size = 0; | |
function getDirsSize($dir_name){ | |
global $size; | |
$dir = scandir($dir_name); | |
unset($dir[0], $dir[1]); | |
foreach($dir as $value){ | |
$str = $dir_name; | |
if(is_file($dir_name.'/'.$value)){ | |
$size += round(filesize($dir_name.'/'.$value)/1024/1024); | |
} | |
if(is_dir($dir_name.'/'.$value)){ | |
$str .= '/'.$value; | |
$arr[$value] = getDirsSize($str); | |
} | |
} | |
return $size; | |
} | |
echo getDirsSize('test'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment