Why does foreach stop after include (second)? Here is my code:
define('MONITORING_MOD', dirname(__FILE__) . '/');
$entries = scandir(MONITORING_MOD);
foreach($entries as $entry) {
if (preg_match('%[a-zA-Z0-9_-]%', $entry)) {
if(is_file(MONITORING_MOD . $entry) && $entry != 'init.php') {
include_once(MONITORING_MOD . $entry);
} else if ($entry != 'init.php') {
$sub_entries = scandir(MONITORING_MOD . $entry);
foreach($sub_entries as $sub_entry) {
echo MONITORING_MOD . $entry . '/' . $sub_entry;
if (preg_match('%[a-zA-Z0-9_-]%', $sub_entry)) {
include(MONITORING_MOD . $entry . '/' . $sub_entry);
}
}
}
}
}
I tried to use include_once, making special function and it did not help.
Another include_once in this code works perfectly and outside foreach too.
Related
I want to copy a whole folders/files tree from one location to another, but for a particular depth I want to perform a transliteration of the destination folder from one language to another.
So for /src/depth1/depth2/depth3/depth4/file I want to transliterate all depth3 folders to another language before copying them over to the destination path.
So I found this little, robust recursive function, which should help me in my task, and then I tried to add a depth control feature so that the transliteration replacement takes place only on depth3 folders. But at first, what I did only added to the depth. Then I thought I found the correct place to add the $depth--;, but unfortunately I can't figure out where to subtract from it in order to start over a new branch...
Could someone help figure this out please?
recurse_copy('TOCOPY/', 'TEST/');
function recurse_copy($src,$dst,$depth = 0) {
echo $src . ' ' . $depth.'<br>';
$dir = opendir($src);
#mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
$depth++;
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file, $depth);
} else {
$depth--;
// copy($src . '/' . $file,$dst . '/' . $file); //I commented this line to save time from copying files again and again while I'm experimenting with the depth control...
}
}
}
closedir($dir);
}
Not tested, but if my understanding is right this should work, plus minus 1 depth.
recurse_copy('TOCOPY/', 'TEST/', 3);
function recurse_copy($src, $dst, $depth = 0) {
echo $src . ' ' . $depth . '<br>';
if ($depth <= 0) {
return;
}
$dir = opendir($src);
#mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file, $depth - 1);
} else {
// copy($src . '/' . $file,$dst . '/' . $file); //I commented this line to save time from copying files again and again while I'm experimenting with the depth control...
}
}
}
closedir($dir);
}
Addition:
This version will rename some directories according to their depth.
recurse_copy_special('TOCOPY/', 'TEST/');
function recurse_copy_special($src, $dst, $depth = 0)
{
echo $src . ' ' . $depth . '<br>';
$base = basename($dst);
$dir = dirname($dst);
if ($depth == 1) {
$dst = $dir . '/' . 'changed1-' . $base;
}
if ($depth == 2) {
$dst = $dir . '/' . 'changed2-' . $base;
}
if ($depth == 3) {
$dst = $dir . '/' . 'changed3-' . $base;
}
$dir = opendir($src);
if (!file_exists($dst)) {
mkdir($dst);
}
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($src . '/' . $file)) {
recurse_copy_special($src . '/' . $file, $dst . '/' . $file, $depth + 1);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
recurse_copy('TOCOPY/', 'TEST/', 3);
function recurse_copy($source, $destination, $depth = 0) {
echo "copy $depth from $source to $destination" . PHP_EOL;
if ($depth < 0) {
return;
}
$directory = opendir($source);
if (!$directory) {
return;
}
#mkdir($destination);
while(false !== ($file = readdir($directory))) {
if (in_array($file, ['.', '..'])) {
continue;
}
$currentSourcePath = $source . '/' . $file;
$currentDestinationPath = $destination . '/' . $file;
if (is_dir($currentSourcePath)) {
recurse_copy($currentSourcePath, $currentDestinationPath, $depth - 1);
continue;
}
copy($currentSourcePath, $currentDestinationPath);
}
closedir($directory);
}
I have one little question, this is my code to list all files from a folder and subfolders;
if ($handle = opendir($dir)) {
$allFiles = array();
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($dir . "/" . $entry)){
$allFiles[] = "D: " . $dir . "/" . $entry;
}else{
$extension = strtoupper(pathinfo($entry, PATHINFO_EXTENSION));
$fileNoExten = pathinfo($entry, PATHINFO_FILENAME);
$directory = substr(str_replace('/', ' > ', $dir), $rootLenOnce + 3);
$listagem .= '<tr>';
$listagem .= "<td><a href='../" . $dir . "/" . $entry . "' ' target='_blank'>" . $entry . "</a></td>";
//$listagem .= "<td><small>" . $directory . "</small></td>";
$listagem .= "<td>" . $extension . "</td>";
$listagem .= "<td><a class='download-cell' href='../".$dir ."/". $entry."' ' download> <i class='fa fa-download' ></i></a></td>";
$listagem .= "<td class='display-none'>" . $fileNoExten . "</td>";
$allFiles[] = "F: " . $dir . "/" . $entry;
$listagem .= '</tr>';
echo "<pre>"; print_r(glob("*.pdf")); echo "</pre>";
}
}
}
closedir($handle);
foreach($allFiles as $value){
$displayName = substr($value, $rootLen + 4);
$fileName = substr($value, 3);
$linkName = str_replace(" ", "%20", substr($value, $pathLen + 3));
if (is_dir($fileName)) {
myScanDirPdf($fileName, $level + 1, strlen($fileName),$rootLenOnce);
}
}
}
return $listagem;
}
what i need is to filtrate the search, to search only .pdf files.
Someone can help me plz!
Thks!
i try with the glob function, but not with great results.
Thks!
When you are looping through each file, you can use
if(stripos($fileName, ".pdf"))
Hope this will help
I have one more suggestion in listing all the files and subfolders.
You can use Recursive Iterator
$folderName = $_POST['folderName'];
$dir = new RecursiveDirectoryIterator($folderName);
$it = new RecursiveIteratorIterator($dir);
foreach ($it as $fileinfo) {
if ($fileinfo->isDir()) {
}elseif ($fileinfo->isFile()) {
$fileName = $fileinfo->getFileName();
if(stripos($fileName, ".pdf")) {
//do what you need to do
}
}
}
I have the following code. I'm looking to do what's in the comment which is to populate a selectbox with a name of directories. How would I do that?
$path = 'repository/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
//extract directory names and populate selectbox
}
}
At maybe its simplest:
echo "<select name=\"something\">";
foreach(glob("repository/{$name[0]}/*", GLOB_ONLYDIR) as $dir) {
echo "<option>$dir</option>";
}
echo "</select>";
A quick and dirty solution, assuming result is the directory you want to show:
echo '<select id="directories" name="directories">';
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
echo '<option value="' . $result . '">' . $result . '</option>';
}
}
echo '</select>';
Perhaps I've not understood the question but surely it's just:
<?php
$selectString = '<select>';
$path = 'repository/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') {
continue;
}
if (is_dir($path . '/' . $result)) {
$selectString .= "<option>{$result}</option>";
}
}
$selectString .= "</select>"
You can then put $selectString wherever you need it.
I have this code for printing js files using php function in my index page:
<? PHP
function admin_js($js, $file = '') {
global $_admin_js;
if ($file != '') {
$file = preg_replace('/\\\/', '/', dirname($file));
}
$_admin_js[] = array(
$js,
$file
);
}
$_admin_js = array();
global $_admin_js;
$value = '';
foreach($_admin_js as $js) {
$relative_path = RELATIVE_PATH;
echo '<script src="' . $relative_path . '/' . preg_replace('/\\\/', '/', $js[0]) . '"></script>' . " \n\t";
}
global $_admin_js;
admin_js('admin/templates/js/jquery-1.10.1.min.js');
admin_js('admin/templates/js/bootstrap.min.js');
?>
But in action php output is empty. whats my problem ? how do print ?!
DEMO HERE
Your code sequence is wrong.
<? PHP
function admin_js($js, $file = '')
{
global $_admin_js;
if ($file != '')
{
$file = preg_replace('/\\\/', '/', dirname($file));
}
$_admin_js[] = array( $js, $file);
}
/* here should be the code of declaration */
$_admin_js = array();
global $_admin_js;
$value = '';
// Call this first to populate the array of $_admin_js
admin_js('admin/templates/js/jquery-1.10.1.min.js');
admin_js('admin/templates/js/bootstrap.min.js');
//Now you get the filled up array...
foreach($_admin_js as $js)
{
$relative_path = RELATIVE_PATH;
echo '<script src="' . $relative_path . '/' . preg_replace('/\\\/', '/', $js[0]) . '"></script>' . " \n\t";
}
I'm using the code here - How can I find unused functions in a PHP project (reproduced below) exactly as it is - just the path modified to my location and it behaves as follows:
root#server [/var/www]# php see_unused_code.php
T_STRING
The code used is:
<?php
$functions = array();
$path = "/path/to/my/php/project";
define_dir($path, $functions);
reference_dir($path, $functions);
echo
"<table>" .
"<tr>" .
"<th>Name</th>" .
"<th>Defined</th>" .
"<th>Referenced</th>" .
"</tr>";
foreach ($functions as $name => $value) {
echo
"<tr>" .
"<td>" . htmlentities($name) . "</td>" .
"<td>" . (isset($value[0]) ? count($value[0]) : "-") . "</td>" .
"<td>" . (isset($value[1]) ? count($value[1]) : "-") . "</td>" .
"</tr>";
}
echo "</table>";
function define_dir($path, &$functions) {
if ($dir = opendir($path)) {
while (($file = readdir($dir)) !== false) {
if (substr($file, 0, 1) == ".") continue;
if (is_dir($path . "/" . $file)) {
define_dir($path . "/" . $file, $functions);
} else {
if (substr($file, - 4, 4) != ".php") continue;
define_file($path . "/" . $file, $functions);
}
}
}
}
function define_file($path, &$functions) {
$tokens = token_get_all(file_get_contents($path));
for ($i = 0; $i < count($tokens); $i++) {
$token = $tokens[$i];
if (is_array($token)) {
if ($token[0] != T_FUNCTION) continue;
$i++;
$token = $tokens[$i];
if ($token[0] != T_WHITESPACE) die("T_WHITESPACE");
$i++;
$token = $tokens[$i];
if ($token[0] != T_STRING) die("T_STRING");
$functions[$token[1]][0][] = array($path, $token[2]);
}
}
}
function reference_dir($path, &$functions) {
if ($dir = opendir($path)) {
while (($file = readdir($dir)) !== false) {
if (substr($file, 0, 1) == ".") continue;
if (is_dir($path . "/" . $file)) {
reference_dir($path . "/" . $file, $functions);
} else {
if (substr($file, - 4, 4) != ".php") continue;
reference_file($path . "/" . $file, $functions);
}
}
}
}
function reference_file($path, &$functions) {
$tokens = token_get_all(file_get_contents($path));
for ($i = 0; $i < count($tokens); $i++) {
$token = $tokens[$i];
if (is_array($token)) {
if ($token[0] != T_STRING) continue;
if ($tokens[$i + 1] != "(") continue;
$functions[$token[1]][1][] = array($path, $token[2]);
}
}
}
?>
PHP version is:
PHP 5.3.3 (cli) (built: Aug 7 2010 14:49:50)
You should look at the source code, read it, understand it. Not just coopy-paste-run. Look at the define_file() function definition: if ($token[0] != T_STRING) die("T_STRING");
You can debug it by displaying the file-path where that condition occurs.
if ($token[0] != T_STRING) die("T_STRING: " . $path . " : " . $token[0]);
Then you'll know what the real problem is.