Skip to main content

File manipulation functionalities

 The following are functions to manipulate with file in PHP.


//Get list of folder in a another folder
function get_list_folder($folder = '', $levels = 100){
if (empty($folder)) return false;
if (!$levels) return false;
$folders = array();
if ($dir = @opendir($folder)) {
while (($file = readdir($dir) ) !== false) {
if (in_array($file, array('.', '..')))
continue;
if (is_dir($folder . '/' . $file)) {
$folders2 = self::get_list_folder($folder . '/' . $file, $levels - 1);
if ($folders2){
$folders = array_merge($folders, $folders2);
}else {
$folders[] = $folder . '/' . $file . '/';
}
}
}
}
@closedir($dir);
return $folders;
}
//Get list of files in a folder
function get_list_files($folder = '', $levels = 100) {
if (empty($folder)) return false;
if (!$levels) return false;
$files = array();
if ($dir = @opendir($folder)) {
while (($file = readdir($dir) ) !== false) {
if (in_array($file, array('.', '..')))
continue;
if (is_dir($folder . '/' . $file)) {
$files2 = self::get_list_files($folder . '/' . $file, $levels - 1);
if ($files2)
$files = array_merge($files, $files2);
else
$files[] = $folder . '/' . $file . '/';
} else {
$files[] = $folder . '/' . $file;
}
}
}
@closedir($dir);
return $files;
}
//Get list of image
function get_list_images($path, $level = 100){
$list = array();
$_list = self::get_list_files($path, $level);
$list = preg_grep('/\.(jpg|jpeg|png|gif)(?:[\?\#].*)?$/i', $_list);
return $list;
}

// Get list of SVG files
function get_list_svgs($path, $level = 100){
$list = array();
$_list = self::get_list_files($path, $level);
$list = preg_grep('/\.(svg)(?:[\?\#].*)?$/i', $_list);
return $list;
}

//Get list of files by type of file
function get_list_files_by_type($path, $level = 100, $type){
$list = array();
$_list = self::get_list_files($path, $level);
$list = preg_grep('/\.(' . $type . ')(?:[\?\#].*)?$/i', $_list);
return $list;
}
//Delete a folder
function delete_folder($path) {
if (is_dir($path) === true) {
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file) {
self::delete_folder(realpath($path) . '/' . $file);
}
return rmdir($path);
} else if (is_file($path) === true) {
return unlink($path);
}
return false;
}

//Copy directory
function copy_dir($src, $dst) {
if (file_exists($dst)) self::delete_folder($dst);
if (is_dir($src)) {
wp_mkdir_p($dst);
$files = scandir($src);
foreach ($files as $file){
if ($file != "." && $file != "..") self::copy_dir("$src/$file", "$dst/$file");
}
} else if (file_exists($src)) copy($src, $dst);
}

//Create dir in wp
function mkdir( $dir ){
if (!file_exists($dir)) {
wp_mkdir_p($dir);
}
}

Comments

Popular posts from this blog

Docker Compose: Node.js Express and MongoDB example

  Docker   provides lightweight containers to run services in isolation from our infrastructure so we can deliver software quickly. In this tutorial, I will show you how to dockerize Nodejs Express and MongoDB example using   Docker Compose .

MUI dialog make sure user can't click on outside when you're opening dialog

< Dialog       open = { open }       onClose = { ( _event , reason ) => {         if ( reason === "backdropClick" || reason === "escapeKeyDown" ) {           return ;         }         onClose ?.();       } }       maxWidth = { maxWidth }       fullWidth       PaperProps = { {         className : "rounded-lg shadow-lg" ,       } }       disableEscapeKeyDown     > { if (reason === "backdropClick" || reason === "escapeKeyDown") { return; } onClose?.(); }} maxWidth={maxWidth} fullWidth PaperProps={{ className: "rounded-lg shadow-lg", }} disableEscapeKeyDown >

Laravel API Tutorial: How to Build and Test a RESTful API

  Laravel is a PHP framework developed with developer productivity in mind. Written and maintained by Taylor Otwell, the framework is very opinionated and strives to save developer time by favoring convention over configuration. The framework also aims to evolve with the web and has already incorporated several new features and ideas in the web development world—such as job queues, API authentication out of the box, real-time communication, and much more. In this article, we’ll explore the ways you can build—and test—a robust API using Laravel. We’ll be using Laravel 5.4, and all of the code is available for reference on GitHub.