0
First I’ll explain my situation:
I have a file called lib.php that has several functions and classes, and I load it into every php file.
In this file I have the function body(), which I call right after the tag <body>
in every file, it returns the html of a header and calls the init.php file, which does the following::
within it has an array with a list of all files within the project folder
take this list and check if any files are missing
scans all folders and subfolders and checks for any file that has not been listed
However, there is also an array that contains folders that are not scanned. Up to there all beauty, I tested numerous times this file accessing it directly, I caused some purposeful errors and it detected cool, played the log on screen, etc, accessing it http://.../init.php
However, when I call this file inside the body() function of the lib.php file, it gives a very strange error:
Warning: Invalid argument supplied for foreach() in C: wamp www oficina init.php on line 111
here is the body() code in lib.php:
function body(){
$return = '';
if(file_exists('init.php')){
require 'init.php';
}else{
$return .= '<b>
<font color="red">
SISTEMA DE DETECÇÃO DE ERROS COMPROMETIDO!
</font>
</b>
<br />
Detectado por function body() em lib.php';
}
// resto da função
return $return; // ele imprime esse retorno usando echo body();
}
here is the code that is giving error in init.php:
$list = array(); //lista de arquivos
$list[] = 'C:/wamp/www/oficina/img/bt_Lembrete.png';
$list[] = 'C:/wamp/www/oficina/img/bt_lembreteNew.png'; // muitos outros arquivos
$noScann = array(); // pastas para não escanear
$noScann[] = 'C:/wamp/www/oficina/noScann';
$noScann[] = 'C:/wamp/www/oficina/js/lib/ui';
$noScann[] = '.';
$noScann[] = '..';
function scan($dir){
global $noScann;
var_dump($noScann); // VAR_DUMP ******************
$files = array();
$d = dir($dir);
while(($entry = $d->read()) !== false){
$continue = false;
foreach($noScann as $arq){ // ERRO NESSA LINHA ***********
if($d->path.$entry == $arq || $entry == $arq){
$continue = true;
}
}
if($continue){
continue;
}
if(is_dir($dir.'/'.$entry)){
$files[] = $d->path.$entry.'/';
$files = array_merge($files, scan($d->path.$entry.'/'));
}else{
$files[] = $d->path.$entry;
}
}
$d->close();
return $files;
}
var_dump($noScann); // VAR_DUMP ******************
// Resto do arquivo irrelevante
The problem is with the variable noScann, which is where I declare the folders not to scan. As you’ve noticed, it’s an array, and I create it outside the function, and within the function I call with global $noScann.
The var_dump inside the function always returns null, and outside the function returns the right values, it’s like I haven’t called it inside the function as global.
If it was a normal mistake, I wouldn’t have asked for help, I did a lot of research, but I didn’t. What I find very strange is that it only gives this error when I call the file inside the body() function of lib.php, and when I access the file directly (http://.../lib.php) it does not give any error, it works perfectly.
I thank you in advance for any attempt to help me. Note: First post aq on stackoverflow, anything wrong correct me
It’s no simpler to pass
$noScann
for the scan() function than declaring it as global?– rray
did not test why the global access to $noScann variable did not work.. but I understood that the reason for using it is to avoid scanning the specific paths in the $noScann array. If so, uncomplicate it and pass it as a parameter. This will eliminate a problem. Another point is the use of foreach($noScann as $Arq) .. I think it’s unnecessary.. could just make use of in_array().
– Daniel Omine
As @rray said, I passed
$noScann
to the function and it worked, thank you very guy.– ayrtonvwf
And actually I didn’t know in_array() @Danielomine, I’ll modify and use it, much simpler and can even save memory for what I saw
– ayrtonvwf