File: //homepages/oneclick/WordPress/6.9/601/scripts/core/file.php
<?php
namespace Core;
set_time_limit(600);
use \Exception;
class File
{
public static function readFile($file)
{
if (!file_exists($file)) {
throw new Exception(
sprintf('File "%s" does not exist.', $file)
);
}
return fread(fopen($file, 'r'), filesize($file));
}
public static function writeFile($file, $content)
{
$fp = fopen($file, 'wb');
if (!$fp) {
throw new Exception(
sprintf('Unable to write file "%s".', $file)
);
}
fputs($fp, $content, strlen($content));
fclose($fp);
}
public static function checkEmptyFolder($dir)
{
if (!is_readable($dir)) {
throw new Exception("Directory ($dir) not readable.");
}
$handle = opendir($dir);
while (false !== ($entry = readdir($handle))) {
if ($entry != '.' && $entry != '..') {
return false;
}
}
closedir($handle);
return true;
}
public static function removeDirectory($dir)
{
if (true === self::checkEmptyFolder($dir)) {
rmdir($dir);
} else {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != '.' && $object != '..') {
if (filetype($dir . '/' . $object) == 'dir') {
self::removeDirectory($dir . '/' . $object);
} else {
unlink($dir . '/' . $object);
}
}
}
reset($objects);
rmdir($dir);
}
}
}
public static function copyDirectory($source, $destination)
{
if (!file_exists($source)) {
throw new Exception("Source ($source) doesn't exist.");
}
if (is_file($source)) {
if (false === copy($source, $destination)) {
throw new Exception('Permission denied.');
}
} elseif (is_dir($source)) {
$dir = opendir($source);
if (!is_readable($destination)) {
if (false === mkdir($destination, 0777, true)) {
throw new Exception('Permission denied.');
}
}
while (false !== ($file = readdir($dir))) {
if ($file != '.' && $file != '..') {
self::copyDirectory($source . '/' . $file, $destination . '/' . $file);
}
}
closedir($dir);
}
}
public static function moveDirectory($source, $destination)
{
self::copyDirectory($source, $destination);
self::removeDirectory($source);
}
public static function searchForFile($dir, $target)
{
$files = scandir($dir);
$list = array();
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
if ($file === $target) {
$list[] = $dir . '/' . $file;
}
if (is_dir($dir . '/' . $file)) {
$subList = self::searchForFile($dir . '/' . $file, $target);
$list = array_merge($list, $subList);
}
}
}
return $list;
}
}