Inside a condition, I have some element in static.
$group == 'modules_account_customers'
for example
I would like to make that in dynamic (read).
How to proceed?
Below you an example of this condition.
All the directories start with modules_
I suppose I must use a function like a glob and select all the _modules. I don't know exactly how to start.
Any idea.
Tk.
foreach ( $modules_array as $module ) {
$class = basename($module, '.php');
// module language
if ( !class_exists($class) ) {
if ( MODE_VENTE_PRIVEE == 'true' && $customer->isLoggedOn() ) {
$modules_boxes = 'modules_boxes';
} elseif (MODE_VENTE_PRIVEE == 'true' && !$customer->isLoggedOn ) {
$modules_boxes = '';
} else {
$modules_boxes = 'modules_boxes';
}
if ($group == $modules_boxes ||
$group == 'modules_account_customers' ||
$group == 'modules_account_history_info' ||
$group == 'modules_advanced_search' ||
$group == 'modules_blog' ||
$group == 'modules_blog_content' ||
$group == 'modules_create_account' ||
) { .....}
}
}
I started by this and allow to display the directories inside an array :
public function getReadModulesDefaultDirectories($source_folder = 'modules_') {
$dir = $this->_directoryTemplateSources . '/' . $this->_template . '/' . $this->_codeSail . '/' . $this->_directoryModules;
$module_directories = array_diff(glob($dir . $source_folder . '*', GLOB_ONLYDIR), $exclude);
$result = [];
foreach ($module_directories as $value) {
$result[] = str_replace($dir, '', $value);
}
return $result;
}
Related
Last time I noticed 2 kind of errors when trying to load the list of images in my uploads folder with Roxy Fileman:
Error loading php/fileslist.php?d=%2Fadmin%2Fuploads&type=image
and sometimes only this error:
E_LoadingAjax php/dirtree.php
So once I have the first error, another time second error after I try to refresh the filemanager window.
After 2nd or 3rd refresh it suddenly works and the files are loaded and shown, but those errors are coming up each time I open the filemanager. randomly they are gone and some time it works, I am not sure what is causing this.
This is the content of filelist.php:
verifyAction('FILESLIST');
checkAccess('FILESLIST');
$path = (empty($_POST['d'])? getFilesPath(): $_POST['d']);
$type = (empty($_POST['type'])?'':strtolower($_POST['type']));
if($type != 'image' && $type != 'flash')
$type = '';
verifyPath($path);
$files = listDirectory(fixPath($path), 0);
natcasesort($files);
$str = '';
echo '[';
foreach ($files as $f){
$fullPath = $path.'/'.$f;
if(!is_file(fixPath($fullPath)) || ($type == 'image' && !RoxyFile::IsImage($f)) || ($type == 'flash' && !RoxyFile::IsFlash($f)))
continue;
$size = filesize(fixPath($fullPath));
$time = filemtime(fixPath($fullPath));
$w = 0;
$h = 0;
if(RoxyFile::IsImage($f)){
$tmp = #getimagesize(fixPath($fullPath));
if($tmp){
$w = $tmp[0];
$h = $tmp[1];
}
}
$str .= '{"p":"'.mb_ereg_replace('"', '\\"', $fullPath).'","s":"'.$size.'","t":"'.$time.'","w":"'.$w.'","h":"'.$h.'"},';
}
$str = mb_substr($str, 0, -1);
echo $str;
echo ']';
And this is dirtree.php:
function getFilesNumber($path, $type){
$files = 0;
$dirs = 0;
$tmp = listDirectory($path);
foreach ($tmp as $ff){
if($ff == '.' || $ff == '..')
continue;
elseif(is_file($path.'/'.$ff) && ($type == '' || ($type == 'image' && RoxyFile::IsImage($ff)) || ($type == 'flash' && RoxyFile::IsFlash($ff))))
$files++;
elseif(is_dir($path.'/'.$ff))
$dirs++;
}
return array('files'=>$files, 'dirs'=>$dirs);
}
function GetDirs($path, $type){
$ret = $sort = array();
$files = listDirectory(fixPath($path), 0);
foreach ($files as $f){
$fullPath = $path.'/'.$f;
if(!is_dir(fixPath($fullPath)) || $f == '.' || $f == '..')
continue;
$tmp = getFilesNumber(fixPath($fullPath), $type);
$ret[$fullPath] = array('path'=>$fullPath,'files'=>$tmp['files'],'dirs'=>$tmp['dirs']);
$sort[$fullPath] = $f;
}
natcasesort($sort);
foreach ($sort as $k => $v) {
$tmp = $ret[$k];
echo ',{"p":"'.mb_ereg_replace('"', '\\"', $tmp['path']).'","f":"'.$tmp['files'].'","d":"'.$tmp['dirs'].'"}';
GetDirs($tmp['path'], $type);
}
}
$type = (empty($_GET['type'])?'':strtolower($_GET['type']));
if($type != 'image' && $type != 'flash')
$type = '';
echo "[\n";
$tmp = getFilesNumber(fixPath(getFilesPath()), $type);
echo '{"p":"'. mb_ereg_replace('"', '\\"', getFilesPath()).'","f":"'.$tmp['files'].'","d":"'.$tmp['dirs'].'"}';
GetDirs(getFilesPath(), $type);
echo "\n]";
What I tried:
checked if php-mbstring module is installed, yes it is
in fuctions.inc.php there is a function:
function listDirectory ($ path) {
$ ret = #scandir ($ path);
if ($ ret === false) {
$ ret = array ();
$ d = opendir ($ path);
if ($ d) {
while (($ f = readdir ($ d))! == false) {
$ ret [] = $ f;
}
closedir ($ d);
}
}
I replaced #scandir with scandir there, did not help.
in system.inc.php is this:
define('BASE_PATH', dirname (FILE));
date_default_timezone_set('UTC');
mb_internal_encoding("UTF-8");
mb_regex_encoding(mb_internal_encoding());
I replaced define('BASE_PATH', dirname (__FILE__)); to define('BASE_PATH', (__DIR__));
Did not help. No more ideas. The dev tools Network neither Console section doesn't show any error.
there,
I tried many time to put the answer from the recursive function inside an array ($result) but I couldn't. Here the code:
function readDirs($path , $result = [])
{
$dirHandle = opendir($path);
while($item = readdir($dirHandle))
{
$newPath = $path."/".$item;
if(is_dir($newPath) && $item != '.' && $item != '..')
{
readDirs($newPath, $result);
}
elseif(!is_dir($newPath) && $item != '.DS_Store' && $item != '.' && $item != '..')
{
echo "$path<br>";
$result[] = $path;
return $result;
}
}
}
$path = "/Users/mycomputer/Documents/www/Photos_projets";
$results = array();
readDirs($path, $results);
Can you help me to put the path inside the array, because I need them later in my code?
Thank's
I cant understand your question but for sure this may be help
function readDirs($path)
{
$result = [];
$dirHandle = opendir($path);
while($item = readdir($dirHandle))
{
$newPath = $path."/".$item;
if(is_dir($newPath) && $item != '.' && $item != '..')
{
$result[] = readDirs($newPath);
}
elseif(!is_dir($newPath) && $item != '.DS_Store' && $item != '.' && $item != '..')
{
echo "$path<br>";
$result[] = $path;
return $result; //im not sure this return statement you implement is needed try it to remove it if this does not work
}
}
return $result;
}
$path = "/Users/mycomputer/Documents/www/Photos_projets";
$result = readDirs($path);
var_dump($result);
try it and response.
I want to ask - How can I return/make array with-in a foreach loop?
Here is my code
function getAllUsers() {
$dir = "users/";
$fl = scandir($dir);
$cnt = 0;
foreach (scandir($dir) as $fl) {
if ($fl !== '.') {
if ($fl !== '..') {
if ($fl !== 'index.php') {
$pr = $fl;
$fl1 = scandir($dir . '/' . $fl);
$cnt++;
return $fl1[3]; //Here I want a array for file inside 'users\'.
}
}
}
}
}
Thanks for giving answer.
Basically: you can not.
upon using return, the current function returns the value and stops execution.
return is by definition the end of the function.
but you can save the data you want to return in an array and then return the whole thing:
function getAllUsers() {
$dir = "users/";
$fl = scandir($dir);
$cnt = 0;
$result = []; //initiating the array
foreach (scandir($dir) as $fl) {
if ($fl !== '.') {
if ($fl !== '..') {
if ($fl !== 'index.php') {
$pr = $fl;
$fl1 = scandir($dir . '/' . $fl);
$cnt++;
$result[] = $fl1[3]; //adding your value to the array
}
}
}
}
return $result;
}
Edit: If your PHP-Version mucks about the $result = []; you can use the "old" style $result = array(); instead
Generators: Solution for PHP >= 5.5.
If you want the function to return a value for each element in the loop back from the function use a generator:
/**
* #return Generator|Iterator
*/
function getAllUsers() {
$dir = "users/";
$fl = scandir($dir);
$cnt = 0;
foreach (scandir($dir) as $fl) {
if ($fl !== '.') {
if ($fl !== '..') {
if ($fl !== 'index.php') {
$pr = $fl;
$fl1 = scandir($dir . '/' . $fl);
$cnt++;
yield $fl1[3]; //Here I want a array for file inside 'users\'.
}
}
}
}
}
// Loop over
foreach (getAllUsers() as $file) {
}
// Or one at a time
$generator = getAllUsers();
$file1 = $generator->current();
The other solutions here are more feasible however, this solution is mostly useful for implementing the IteratorAggregate interface, or creating co-routines.
You can achieve this by using an array. like below..
function getAllUsers() {
$dir = "users/";
$fl = scandir($dir);
$cnt = 0;
$array = array();
foreach (scandir($dir) as $fl) {
if ($fl !== '.') {
if ($fl !== '..') {
if ($fl !== 'index.php') {
$pr = $fl;
$fl1 = scandir($dir . '/' . $fl);
$cnt++;
$array[$fl] = $fl1[3]; //Push values in array for each user.
}
}
}
}
return $array;
}
Hope this will help
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"
I've the following code:
if ($type == 'unit'){
$item_title = $result["title"];
}
elseif ($type == 'message'){
$item_title = $result["description"];
}
// all should combine unit and message
elseif ($type == 'all'){
$item_title = $result["description"]
$item_title .= $result["title"];
}
if (stripos( $item_title, $filter ) !== false || stripos( $item_title, $filter ) !== false)
How can I combine the unit and message results in the elseif ($type == all) statement?
I'm not sure what you mean, is this what you want?
elseif ($type == 'all'){
$item_title = $result["description"] . $result["title"];
}
You could create an associative array to store both....
elseif ($type == 'all') {
$item_title = array(
'description' => $result["description"],
'title' => $result["title"]
);
}
You can then access them with $item_title['description'] and $item_title['title'].
If you need to determine if $item_title is an array or not, use is_array().