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"
Related
I want to get all file names (and path) of files those updated after a date (in directory and subdirectories) using PHP.
like all files updated after 20.08.2017 ,
Below code provide only files from directory, i also need path,
$dir = "opendir(".")";
clearstatcache();
$yesdate = strtotime("-1 days");
while(false != ($file = readdir($dir)))
{
if ( substr($file,-4) == ".php" )
{
if (filemtime($file) >= $yesdate)
{
echo $file;
}
}
}
Thanks
If you're using relative paths like e.g. . or paths that follow a symbolic link, you can get the real path via the function realpath :
$actualDirectory = realpath(".");
$dir = opendir($actualDirectory);
clearstatcache();
$yesdate = strtotime("-1 days");
while(false != ($file = readdir($dir)))
{
if ( substr($file,-4) == ".php" && filemtime($file) >= $yesdate)
{
echo $actualDirectory."/".$file;
}
}
If you want to scan all sub directories until the end of the tree you need to use a recursive function.
function get_updated_files($date, $directory, $result = array())
{
$directory = realpath($directory);
$directory_content = glob($directory.'/*');
foreach($directory_content as $item) {
if(is_dir($item)) {
$result = get_updated_files($date, $item, $result);
} elseif(strtotime($date) < filemtime($item) && pathinfo($item, PATHINFO_EXTENSION) == 'php') {
$result[] = $item;
}
}
return $result;
}
$result = get_updated_files('2017-08-20', '.');
With a specified date:
$dir = opendir(dirname(__FILE__));
clearstatcache();
$dday = "20.08.2017.";
$yesdate = strtotime($dday);
while(false != ($file = readdir($dir))) {
if ( substr($file,-4) == ".php" ) {
if (filemtime($file) >= $yesdate) {
echo $file;
}
}
}
Or with today - 1 day:
$dday = date("d.m.Y", time());
$yesdate = strtotime($dday) - 86400;
You must proceed like this
function get_updated_files($date, $directory,$file_extension='php',$sameday=false, $result = array())
{
if(!$sameday){
/* really worth because actually
your code will return true for your given day and this maybe
is not the goal you are trying to achieve...*/
$date = strtotime($date)+86399;
}else{
$date=strtotime($date);
}
$directory = realpath($directory);
$directory_content = glob($directory.'\\*');
foreach($directory_content as $item) {
if(is_dir($item)) {
$result=get_updated_files($date, $item,$file_extension,$sameday,$result);
} elseif($date < filemtime($item)&& pathinfo($item, PATHINFO_EXTENSION)===$file_extension) {
$result[] = $item;
}
}
return $result;
}
$result = get_updated_files('2017-08-20', '.');
I would like to ask what I have to add to make this function to show not only the files on top dir but also the files in subdirs..
private function _populateFileList()
{
$dir_handle = opendir($this->_files_dir);
if (! $dir_handle)
{
return false;
}
while (($file = readdir($dir_handle)) !== false)
{
if (in_array($file, $this->_hidden_files))
{
continue;
}
if (filetype($this->_files_dir . '/' . $file) == 'file')
{
$this->_file_list[] = $file;
}
}
closedir($dir_handle);
return true;
}
Thank you in advance!
You could implement the recursion yourself, or you could use the existing iterator classes to handle the recursion and filesystem traversal for you:
$dirIterator = new RecursiveDirectoryIterator('.', FilesystemIterator::SKIP_DOTS);
$recursiveIterator = new RecursiveIteratorIterator($dirIterator);
$filterIterator = new CallbackFilterIterator($recursiveIterator, function ($file) {
// adjust as needed
static $badFiles = ['foo', 'bar', 'baz'];
return !in_array($file, $badFiles);
});
$files = iterator_to_array($filterIterator);
var_dump($files);
By this you can get all subdir content
customerdel('FolderPath');
function customerdel($dirname=null){
if($dirname!=null){
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
echo $dirname."/".$file.'<br>';
else{
echo $dirname.'/'.$file.'<br> ';
customerdel($dirname.'/'.$file);
}
}
}
closedir($dir_handle);
}
}
Here is how you can get a recursive array of all files in a directory and its subdirectories.
The returned array is like: array( [fileName] => [filePath] )
EDIT: I've included a small check if there are filenames in the subdirectories with the same name. If so, an underscore and counter is added to the key-name in the returned array:
array( [fileName]_[COUNTER] => [filePath] )
private function getFileList($directory) {
$fileList = array();
$handle = opendir($directory);
if ($handle) {
while ($entry = readdir($handle)) {
if ($entry !== '.' and $entry !== '..') {
if (is_dir($directory . $entry)) {
$fileList = array_merge($fileList, $this->getFileList($directory . $entry . '/'));
} else {
$i = 0;
$_entry = $entry;
// Check if filename is allready in use
while (array_key_exists($_entry, $fileList)) {
$i++;
$_entry = $entry . "_$i";
}
$fileList[$_entry] = $directory . $entry;
}
}
}
closedir($handle);
}
return $fileList;
}
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...
I have a huge ammount of photos that need sorting through. I need to know the dimensions of each photo in order to know or it needs re-sizing. As a programmer I'm convinced there must be a quicker way of doing this.
I got quite far. The following code reads the dir and all the sub dirs. But the moment I try to extract the dimensions the loop halts at 8% of all the pictures that need checking. Could it be PHP is not allowed to do more calculations? What is going on!?
This is how far I got:
checkDir('dir2Check');
function checkDir($dir, $level = 0) {
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if (!preg_match('/\./i', $entry)) {
echo echoEntry("DIR\\", $entry, $level);
checkDir($dir.'/'.$entry, $level+1);
} else {
if ($entry != "." && $entry != ".." && $entry != ".DS_Store") {
// if I comment the next line. It loops through all the files in the directory
checkFile($entry, $dir.'/'.$entry, $level);
// this line echoes so I can check or it really read all the files in case I comment the proceeding line
//echo echoEntry("FILE", $entry, $level);
}
}
}
$level--;
closedir($handle);
}
}
// Checks the file type and lets me know what is happening
function checkFile($fileName, $fullPath, $level) {
if (preg_match('/\.gif$/i', $fullPath)) {
$info = getImgInfo(imagecreatefromgif($fullPath));
} else if (preg_match('/\.png$/i', $fullPath)) {
$info = getImgInfo(imagecreatefrompng($fullPath));
} else if (preg_match('/\.jpe?g$/i', $fullPath)){
$info = getImgInfo(imagecreatefromjpeg($fullPath));
} else {
echo "XXX____file is not an image [$fileName]<br />";
}
if ($info) {
echo echoEntry("FILE", $fileName, $level, $info);
}
}
// get's the info I need from the image and frees up the cache
function getImgInfo($srcImg) {
$width = imagesx($srcImg);
$height = imagesy($srcImg);
$info = "Dimensions:".$width."X".$height;
imagedestroy($srcImg);
return $info;
}
// this file formats the findings of my dir-reader in a readable way
function echoEntry($type, $entry, $level, $info = false) {
$output = $type;
$i = -1;
while ($i < $level) {
$output .= "____";
$i++;
}
$output .= $entry;
if ($info) {
$output .= "IMG_INFO[".$info."]";
}
return $output."<br />";
}
The following does similar to what you do, only it's using php's DirectoryIterator which in my humble opinion is cleaner and more OOP-y
<?php
function walkDir($path = null) {
if(empty($path)) {
$d = new DirectoryIterator(dirname(__FILE__));
} else {
$d = new DirectoryIterator($path);
}
foreach($d as $f) {
if(
$f->isFile() &&
preg_match("/(\.gif|\.png|\.jpe?g)$/", $f->getFilename())
) {
list($w, $h) = getimagesize($f->getPathname());
echo $f->getFilename() . " Dimensions: " . $w . ' ' . $h . "\n";
} elseif($f->isDir() && $f->getFilename() != '.' && $f->getFilename() != '..') {
walkDir($f->getPathname());
}
}
}
walkDir();
You can simply use getimagesize()
list($width, $height) = getimagesize($imgFile);
<?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.