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 .

HƯỚNG DẪN THIẾT KẾ WEB CHUẨN SEO

  Hướng dẫn thiết kế web chuẩn seo .  Thế nào là thiết kế web chuẩn seo? Tại sao bạn cần phải biết thiết kế web chuẩn seo. Bởi vì, chuẩn seo là một phần rất quan trọng của một trang thiết kế web. Bạn muốn tìm được khách hàng? Muốn từ khóa bạn nhanh lên TOP thì điều đầu tiên bạn cần làm là trang web của bạn được thiết kế chuẩn seo. Không chỉ chuẩn seo về trang web mà bài viết cũng cần được tối ưu hóa cho chuẩn seo. Có bao nhiêu yếu tố để thiết kế được trang web chuẩn seo?

Sample VS code setting

  When you are a software developer, you need to configure your IDE working with most convinience for your working, bellow is a sample code snippet to config your IDE {   "diffEditor.ignoreTrimWhitespace" : false ,   "javascript.updateImportsOnFileMove.enabled" : "always" ,   "[typescriptreact]" : {       "editor.defaultFormatter" : "esbenp.prettier-vscode"   },   "editor.formatOnPaste" : true ,   "workbench.settings.applyToAllProfiles" : [],   "editor.tabSize" : 2 ,   "redhat.telemetry.enabled" : true ,   "editor.codeActionsOnSave" : {       },   // "editor.codeActionsOnSave": {   //   "source.fixAll": "explicit",   //   "source.fixAll.eslint": "explicit",   //   "source.organizeImports": "explicit",   //   "source.sortMembers": "explicit",   //   "javascript.showUnused": "...