WebsiteBaker Support (2.13.x) > General Help & Support
'scandir' and 'foreach' in code module
netraam:
Hello every one,
I've just updated from 2.12 to 2.13.
I have a page i've wrote in the code module to display the images in 4 folders but after the update it doesn't work anny more.
does somone know what is wrong?
below is my code:
--- Code: ---
$map4 = "../../modules/sponsors/sponsors4/";
$map3 = "../../modules/sponsors/sponsors3/";
$map2 = "../../modules/sponsors/sponsors2/";
$map1 = "../../modules/sponsors/sponsors1/";
$bestanden4 = scandir($map4);
$bestanden3 = scandir($map3);
$bestanden2 = scandir($map2);
$bestanden1 = scandir($map1);
echo "<div style=\"text-align: center;\">";
foreach ($bestanden4 as $foto){
echo 'test';
if (substr($foto, -4)=='.jpg'|| substr($foto, -5)=='.jpeg' || substr($foto, -4)=='.JPG' || substr($foto, -4)=='.png'){
$info = pathinfo($foto);
$filename = basename($foto,'.'.$info['extension']);
if (file_exists("$map4/$filename.txt")){
$file = fopen("$map4/$filename.txt","r");
$website = fgets($file);
echo "\n <a href='$website' target='_blank'><img src=\"$map4/$foto\" alt=\"\" style=\"width: 220px; margin: 20px;\"/></a>";
fclose($file);
}
else{
echo "\n <img src=\"$map4/$foto\" alt=\"\" style=\"width: 220px; margin: 20px;\"/>";
}
}
}
foreach ($bestanden3 as $foto){
if (substr($foto, -4)=='.jpg'|| substr($foto, -5)=='.jpeg' || substr($foto, -4)=='.JPG' || substr($foto, -4)=='.png'){
$info = pathinfo($foto);
$filename = basename($foto,'.'.$info['extension']);
if (file_exists("$map3/$filename.txt")){
$file = fopen("$map3/$filename.txt","r");
$website = fgets($file);
echo "\n <a href='$website' target='_blank'><img src=\"$map3/$foto\" alt=\"\" style=\"width: 220px; margin: 20px;\"/></a>";
fclose($file);
}
else{
echo "\n <img src=\"$map3/$foto\" alt=\"\" style=\"width: 220px; margin: 20px;\"/>";
}
}
}
foreach ($bestanden2 as $foto){
if (substr($foto, -4)=='.jpg'|| substr($foto, -5)=='.jpeg' || substr($foto, -4)=='.JPG' || substr($foto, -4)=='.png'){
$info = pathinfo($foto);
$filename = basename($foto,'.'.$info['extension']);
if (file_exists("$map2/$filename.txt")){
$file = fopen("$map2/$filename.txt","r");
$website = fgets($file);
echo "\n <a href='$website' target='_blank'><img src=\"$map2/$foto\" alt=\"\" style=\"width: 220px; margin: 20px;\"/></a>";
fclose($file);
}
else{
echo "\n <img src=\"$map2/$foto\" alt=\"\" style=\"width: 220px; margin: 20px;\"/>";
}
}
}
foreach ($bestanden1 as $foto){
if (substr($foto, -4)=='.jpg'|| substr($foto, -5)=='.jpeg' || substr($foto, -4)=='.JPG' || substr($foto, -4)=='.png'){
$info = pathinfo($foto);
$filename = basename($foto,'.'.$info['extension']);
if (file_exists("$map1/$filename.txt")){
$file = fopen("$map1/$filename.txt","r");
$website = fgets($file);
echo "\n <a href='$website' target='_blank'><img src=\"$map1/$foto\" alt=\"\" style=\"width: 220px; margin: 20px;\"/></a>";
fclose($file);
}
else{
echo "\n <img src=\"$map1/$foto\" alt=\"\" style=\"width: 220px; margin: 20px;\"/>";
}
}
}
echo "</div>";
--- End code ---
thanks in advance,
Maarten
sternchen8875:
Hi Maarten
the code works for me, if i change a little bit
take a look into the error.log (X-Button in Top-Menu of the Backend) and you find messages like this
--- Quote ---scandir(........./modules/sponsors/sponsors4/): Failed to open directory: not implemented"
--- End quote ---
scandir() needs a path like this
--- Code: ---$bestanden4 = scandir(WB_PATH.$map4);
$bestanden3 = scandir(WB_PATH.$map3);
$bestanden2 = scandir(WB_PATH.$map2);
$bestanden1 = scandir(WB_PATH.$map1);
--- End code ---
and the img-Tag needs a URL, absolute or relative URL. "relative" means, Startpoint is the index.php in the WB_Root-Folder. Use a definition like this for $map1 - $map4
--- Quote ---$map4 = "/modules/sponsors/sponsors4/";
$map3 = "/modules/sponsors/sponsors3/";
$map2 = "/modules/sponsors/sponsors2/";
$map1 = "/modules/sponsors/sponsors1/";
--- End quote ---
Same problem with the paths to the txt-file(s) in every foreach-loop
--- Code: ---foreach ($bestanden4 as $foto){
if (substr($foto, -4)=='.jpg'|| substr($foto, -5)=='.jpeg' || substr($foto, -4)=='.JPG' || substr($foto, -4)=='.png'){
$info = pathinfo($foto);
$filename = basename($foto,'.'.$info['extension']);
if (file_exists(WB_PATH.$map4."$filename.txt")){
$file = @fopen(WB_PATH.$map4."$filename.txt","r");
$website = fgets($file);
echo "\n <a href='$website' target='_blank'><img src=\"$map4/$foto\" alt=\"\" style=\"width: 220px; margin: 20px;\"/></a>";
fclose($file);
}
else{
echo "\n <img src=\"$map4/$foto\" alt=\"\" style=\"width: 220px; margin: 20px;\"/>";
}
}
}
--- End code ---
hgs:
Alternatively, the Foldergallery module could be used for the purpose.
It does exactly the job requested.
A preview image is generated for each directory and the link on this preview image shows the images in the respective directory
Here is an example in german
Above are the thumbnails of the subdirectories (4 pieces) below are the thumbnails in the start directory, if this is empty, only the thumbnails of the subdirectories are shown.
netraam:
@sternchen8875
thanks for the your solution. I didn't know exactly how to use the WB variables in the code module.
But now it works.
thank you.
sternchen8875:
(Y)
P.S.: Ruud has a fine module with all in WB used Constant's -> https://dev4me.com/modules-snippets/opensource/sysinfo/
All this works also in Code or CodeĀ²-Module or in selfmade-modules
Navigation
[0] Message Index
[#] Next page
Go to full version