How can I show a folder's limit in a directory using PHP? The code below shows all folders but I only want to see 10 folders.
function folderlist() {
$startdir = './';
$ignoredDirectory[] = '.';
$ignoredDirectory[] = '..';
if(is_dir($startdir)) {
if($dh = opendir($startdir)) {
while(($folder = readdir($dh)) !== false) {
if(!(array_search($folder, $ignoredDirectory) > -1)) {
if(filetype($startdir.$folder) == "dir") {
$mtime = filemtime($startdir.$folder);
$directorylist[$mtime]['name'] = $folder;
$directorylist[$mtime]['path'] = $startdir;
}
}
}
closedir($dh);
}
}
krsort($directorylist, SORT_NUMERIC);
return $directorylist;
}
$folders = folderlist();
foreach($folders as $folder) {
$path = $folder['path'];
$name = $folder['name'];
echo '<div class="urbangreymenu"><ul><li>'.$name.'</li></ul></div>';
}
Change three lines:
function folderlist($limit = 10) {
and ...
while (($folder = readdir($dh)) !== false && $limit) {
and...
$limit--;
Together:
function folderlist($limit = 10) {
$startdir = './';
$ignoredDirectory[] = '.';
$ignoredDirectory[] = '..';
if (is_dir($startdir)) {
if ($dh = opendir($startdir)) {
while (($folder = readdir($dh)) !== false && $limit) {
if (!(array_search($folder,$ignoredDirectory) > -1)) {
if (filetype($startdir . $folder) == "dir") {
$mtime = filemtime($startdir . $folder);
$directorylist[$mtime]['name'] = $folder;
$directorylist[$mtime]['path'] = $startdir;
$limit--;
}
}
}
//Rest of code unchanged...
Related
I have this script that works good, but I need to add a command for searching on all subfolders.
Example: I have a folder data and this contains more another folders... I need to search files on this folders.
$dir = 'data';
$exclude = array('.', '..', '.htaccess');
$q = (isset($_GET['q'])) ? strtolower($_GET['q']) : '';
$res = opendir($dir);
while(false !== ($file = readdir($res))) {
if(strpos(strtolower($file), $q) !== false &&!in_array($file, $exclude)) {
echo "<a href='$dir/$file'>$file</a>";
echo "<br>";
}
}
closedir($res);
you can use scandir() function
$dir = '/tmp';
$file = scandir($dir);
print_r($file);
I think this is the recursive function you are looking for :
function dir_walk($dir, $q) {
$q = trim($q);
$exclude = array('.', '..', '.htaccess');
if($dh = opendir($dir)) {
while(($file = readdir($dh)) !== false) {
if(in_array($file, $exclude)) { continue; }
elseif(is_file($dir.$file)) {
if($q === '' || strpos(strtolower($file), $q) !== false) {
echo '<a href='.$dir.$file.'>'.$dir.$file.'</a><br/>';
}
}
elseif(is_dir($dir.$file)) {
dir_walk($dir.$file.DIRECTORY_SEPARATOR, $q);
}
}
closedir($dh);
}
}
$q = (isset($_GET['q'])) ? strtolower($_GET['q']) : '';
dir_walk('/data/', $q);
Edit: data need to be the absolute path to the main dir, with ending directory separator "/data/"
How do we return the absolute path of largest file in a particular directory?
I've been fishing around and haven't turned up anything concrete?
I'm thinking it has something to do with glob()?
$sz = 0;
$dir = '/tmp'; // will find largest for `/tmp`
if ($handle = opendir($dir)) { // will iterate through $dir
while (false !== ($entry = readdir($handle))) {
if(($curr = filesize($dir . '/' . $entry)) > $sz) { // found larger!
$sz = $curr;
$name = $entry;
}
}
}
echo $dir . '/' . $name; // largest
$dir = 'DIR_NAME';
$max_filesize = 0;
$path= '';
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(realpath($dir),FilesystemIterator::SKIP_DOTS)) as $file){
if ($file->getSize() >= $max_filesize){
$max_filesize = $file->getSize();
$path = $file->getRealPath(); // get absolute path
}
}
echo $path;
function getFileSize($directory) {
$files = array();
foreach (glob($directory. '*.*') as $file) {
$files[] = array('path' => $file, 'size' => filesize($file));
}
return $files;
}
function getMaxFile($files) {
$maxSize = 0;
$maxIndex = -1;
for ($i = 0; $i < count($files); $i++) {
if ($files[$i]['size'] > $maxSize) {
$maxSize = max($maxSize, $files[$i]['size']);
$maxIndex = $i;
}
}
return $maxIndex;
}
usage:
$dir = '/some/path';
$files = getFileSize($dir);
echo '<pre>';
print_r($files);
echo '</pre>';
$maxIndex = getMaxFile($files);
var_dump($files[$maxIndex]);
I've a simple problem of copying a a php folder to some directories, bu the problem is I can't the solution for that, the idea is that I've an Online Manga Viewer script, and what I want to do is I want to add comments page to every chapter, the I dea that I came with, is, I create a separate comments page file and once a new chapter added the the comments file will be copied to the folder of the chapter :
Description Image:
http://i.stack.imgur.com/4wYE0.png
What I to know is how can I do it knowing that I will use Disqus commenting System.
Functions used in the script:
function omv_get_mangas() {
$mangas = array();
$dirname = "mangas/";
$dir = #opendir($dirname);
if ($dir) {
while (($file = #readdir($dir)) !== false) {
if (is_dir($dirname . $file . '/') && ($file != ".") && ($file != "..")) {
$mangas[] = $file;
}
}
#closedir($dir);
}
sort($mangas);
return $mangas;
}
function omv_get_chapters($manga) {
global $omv_chapters_sorting;
$chapters = array();
$chapters_id = array();
$dirname = "mangas/$manga/";
$dir = #opendir($dirname);
if ($dir) {
while (($file = #readdir($dir)) !== false) {
if (is_dir($dirname . $file . '/') && ($file != ".") && ($file != "..")) {
$chapter = array();
$chapter["folder"] = $file;
$pos = strpos($file, '-');
if ($pos === false) {
$chapter["number"] = $file;
} else {
$chapter["number"] = trim(substr($file, 0, $pos - 1));
$chapter["title"] = trim(substr($file, $pos + 1));
}
$chapters_id[] = $chapter["number"];
$chapters[] = $chapter;
}
}
#closedir($dir);
}
array_multisort($chapters_id, $omv_chapters_sorting, $chapters);
return $chapters;
}
function omv_get_chapter_index($chapters, $chapter_number) {
$i = 0;
while (($i < count($chapters)) && ($chapters[$i]["number"] != $chapter_number)) $i++;
return ($i < count($chapters)) ? $i : -1;
}
function omv_get_pages($manga, $chapter) {
global $omv_img_types;
$pages = array();
$dirname = "mangas/$manga/$chapter/";
$dir = #opendir($dirname);
if ($dir) {
while (($file = #readdir($dir)) !== false) {
if (!is_dir($dirname . $file . '/')) {
$file_extension = strtolower(substr($file, strrpos($file, ".") + 1));
if (in_array($file_extension, $omv_img_types)) {
$pages[] = $file;
}
}
}
#closedir($dir);
}
sort($pages);
return $pages;
}
/*function add_chapter_comment($dirname){
$filename = $dirname.'comments.php';
if (file_exists($filename)) {
} else {
copy('comments.php', .$dirname.'comments.php');
}
}*/
function omv_get_previous_page($manga_e, $chapter_number_e, $current_page, $previous_chapter) {
if ($current_page > 1) {
return $manga_e . '/' . $chapter_number_e . '/' . ($current_page - 1);
} else if ($previous_chapter) {
$pages = omv_get_pages(omv_decode($manga_e), $previous_chapter["folder"]);
return $manga_e . '/' . omv_encode($previous_chapter["number"]) . '/' . count($pages);
} else {
return null;
}
}
function omv_get_next_page($manga_e, $chapter_number_e, $current_page, $nb_pages, $next_chapter) {
if ($current_page < $nb_pages) {
return $manga_e . '/' . $chapter_number_e . '/' . ($current_page + 1);
} else if ($next_chapter) {
return $manga_e . '/' . omv_encode($next_chapter["number"]);
} else {
return null;
}
}
function omv_get_image_size($img) {
global $omv_img_resize, $omv_preferred_width;
$size = array();
$imginfo = getimagesize($img);
$size["width"] = intval($imginfo[0]);
$size["height"] = intval($imginfo[1]);
if ($omv_img_resize) {
if ($size["width"] > $omv_preferred_width) {
$size["height"] = intval($size["height"] * ($omv_preferred_width / $size["width"]));
$size["width"] = $omv_preferred_width;
}
}
return $size;
}
And thanks for all of you!
Include the following line in all of your pages in a small php statement, if it covers two folder paths, use this. Which I think in your case it does.
<?php
include('../../header.php');
?>
And then save this in the main root directory. Which in your diagram is called "Main Folder"
The function below returns an array of XML files from a given directory including all subdirectories. How can I modify this function by passing a second optional parameter which excludes a directory.
E.g:
getDirXmlFiles($config_dir, "example2");
Directory/Files
/file1.xml
/file2.xml
/examples/file3.xml
/examples/file4.xml
/example2/file5.xml
/example2/file5.xml
In the above case the function would return all files except files in the example2 directory.
function getDirXmlFiles($base) {
$files = array();
if(!is_dir($base)) return $files;
if ($handle = opendir($base)) {
while (false !== ($file = readdir($handle))) {
if ($file == "." || $file == "..") continue;
if(is_dir("$base/$file")) {
$subfiles = $this->getDirXmlFiles("$base/$file");
$files = array_merge($files, $subfiles);
} else {
if(Cms_File::type($file,false) == "xml")
$files[] = "$base/$file";
}
}
closedir($handle);
}
return $files;
}
Try this:
function getDirXmlFiles($base, $exclude = NULL) {
$files = array();
if(!is_dir($base)) return $files;
if ($handle = opendir($base)) {
while (false !== ($file = readdir($handle))) {
if ($file == "." || $file == ".." || "$base/$file" == $exclude) continue;
if(is_dir("$base/$file")) {
$subfiles = $this->getDirXmlFiles("$base/$file",$exclude);
$files = array_merge($files, $subfiles);
} else {
if(Cms_File::type($file,false) == "xml")
$files[] = "$base/$file";
}
}
closedir($handle);
}
return $files;
}
Redefine the function inputs:
function getDirXmlFiles($base, $excludeDir) {
Then change this line:
if(is_dir("$base/$file")) {
to this:
if(is_dir("$base/$file") && "$base/$file" != "$base/$excludeDir") {
You can just slightly modify the function to check and see if the current directory is the one you want to exclude or not
function getDirXmlFiles($base,$exclude) {
$files = array();
if(!is_dir($base)) return $files;
if ($handle = opendir($base)) {
while (false !== ($file = readdir($handle))) {
if ($file == "." || $file == "..") continue;
if(is_dir("$base/$file") && $file != $exclude) {
$subfiles = $this->getDirXmlFiles("$base/$file",$exclude);
$files = array_merge($files, $subfiles);
} else {
if(Cms_File::type($file,false) == "xml")
$files[] = "$base/$file";
}
}
closedir($handle);
}
return $files;
}
Smth of the sort ( tested only the syntax validation there might be needed some small debugging ) ( this will allow you to exclude multiple directory names )
function getDirXmlFiles($base, $excludeArray = array()) {
if (!is_array($excludeArray))
throw new Exception(__CLASS__ . "->" . __METHOD__ . " expects second argument to be a valid array");
$files = array();
if(!is_dir($base)) return $files;
if ($handle = opendir($base)) {
while (false !== ($file = readdir($handle))) {
$path = $base . "/" . $file;
$isDir = is_dir($path);
if (
$file == "."
|| $file == ".."
|| (
$isDir
&& count($excludeArray) > 0
&& in_array($file, $excludeArray)
)
)
continue;
if($isDir) {
$subfiles = $this->getDirXmlFiles($path, $excludeArray);
$files = array_merge($files, $subfiles);
} else {
if(Cms_File::type($file,false) == "xml")
$files[] = $path;
}
}
closedir($handle);
}
return $files;
}
<?php
function scan_dir($dirname) {
$file_count = 0 ;
$dir_count = 0 ;
$dir = opendir($dirname);
while (($file = readdir($dir)) !== false) {
if($file != "." && $file != "..") {
if(is_file($dirname."/".$file))
++$file_count;
if(is_dir($dirname."/".$file)) {
++ $dir_count;
scan_dir($dirname."/".$file);
}
}
}
closedir($dir);
echo "There are $dir_count catalogues and $file_count files.<br>";
}
$dirname = "/home/user/path";
scan_dir($dirname);
?>
Hello,
I have a recursive function for count files and catalogues. It returns result for each catalogue.
But I need a common result. How to change the script?
It returns :
There are 0 catalogues and 3 files.
There are 0 catalogues and 1 files.
There are 2 catalogues and 14 files.
I want:
There are 2 catalogues and 18 files.
You could tidy up the code a lot with RecursiveDirectoryIterator.
$dirs = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(dirname(__FILE__))
, TRUE);
$dirsCount = $filesCount = 0;
while ($dirs->valid()) {
if ($dirs->isDot()) {
$dirs->next();
} else if ($dirs->isDir()) {
$dirsCount++;
} else if ($dirs->isFile()) {
$filesCount++;
}
$dirs->next();
}
var_dump($dirsCount, $filesCount);
You can return values from each recursive call, and sum those and return back to its caller.
<?php
function scan_dir($dirname) {
$file_count = 0 ;
$dir_count = 0 ;
$dir = opendir($dirname);
$sub_count = 0;
while (($file = readdir($dir)) !== false) {
if($file != "." && $file != "..") {
if(is_file($dirname."/".$file))
++$file_count;
if(is_dir($dirname."/".$file)) {
++ $dir_count;
$sub_count += scan_dir($dirname."/".$file);
}
}
}
closedir($dir);
echo "There are $dir_count catalogues and $file_count files.<br>";
return $sub_count + $dir_count + $file_count;
}
$dirname = "/home/user/path";
echo "Total count is ". scan_dir($dirname);
?>
The code will give you the net count of every item.
With a simple modification. Just, for example, keep the counts in an array that you can return from the function to add up to the previous counts, like so:
<?php
function scan_dir($dirname) {
$count['file'] = 0;
$count['dir'] = 0;
$dir = opendir($dirname);
while (($file = readdir($dir)) !== false) {
if($file != "." && $file != "..") {
if(is_file($dirname."/".$file))
$count['file']++;
if(is_dir($dirname."/".$file)) {
$count['dir']++;
$counts = scan_dir($dirname."/".$file);
$count['dir'] += $counts['dir'];
$count['file'] += $counts['file'];
}
}
}
closedir($dir);
return $count;
}
$dirname = "/home/user/path";
$count = scan_dir($dirname);
echo "There are $count[dir] catalogues and $count[file] files.<br>";
?>
In my opnion, you should separate counting file & counting dir to 2 different function. It will clear things up:
<?php
function scan_dir_for_file($dirname) {
$file_count = 0 ;
$dir = opendir($dirname);
while (($file = readdir($dir)) !== false) {
if($file != "." && $file != "..") {
if(is_file($dirname."/".$file))
{
++$file_count;
} else {
$file_count = $file_count + scan_dir($dirname."/".$file);
}
}
}
return $file_count
}
?>
The directory_count function is similar.