WebsiteBaker Community Forum

WebsiteBaker Support (2.13.x) => General Help & Support => Topic started by: netraam on September 21, 2023, 09:35:45 PM

Title: 'scandir' and 'foreach' in code module
Post by: netraam on September 21, 2023, 09:35:45 PM
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: [Select]

        $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>";

thanks in advance,
Maarten
Title: Re: 'scandir' and 'foreach' in code module
Post by: sternchen8875 on September 22, 2023, 01:44:45 AM
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"

scandir() needs a path like this

Code: [Select]
$bestanden4 = scandir(WB_PATH.$map4);
$bestanden3 = scandir(WB_PATH.$map3);
$bestanden2 = scandir(WB_PATH.$map2);
$bestanden1 = scandir(WB_PATH.$map1);

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/";

Same problem with the paths to the txt-file(s) in every foreach-loop

Code: [Select]
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;\"/>";
}
}
}


Title: Re: 'scandir' and 'foreach' in code module
Post by: hgs on September 22, 2023, 07:03:00 AM
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 (https://82.umojasingers.de/pages/module-ohne-probleme/fg-4-0-3.php)

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.
Title: Re: 'scandir' and 'foreach' in code module
Post by: netraam on September 23, 2023, 12:42:09 AM
@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.
Title: Re: 'scandir' and 'foreach' in code module
Post by: sternchen8875 on September 23, 2023, 12:39:14 PM
 (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
Title: Re: 'scandir' and 'foreach' in code module
Post by: DarkViper on September 26, 2023, 09:25:13 AM
[…] I didn't know exactly how to use the WB variables in the code module.[…]

The new programming style (also at WebsiteBaker) largely avoids global constants and especially global variables, as their use usually contradicts general coding standards.

Here at WebsiteBaker there is an object that provides the necessary data (e.g. previous constants, etc.) and global objects (Database, Translate, Application, Requester) in a secure manner.
A description of this object can be found at the following link:

WB-Wiki (WbAdaptor) (https://wiki.WebsiteBaker.org/doku.php/en/dev/284/registry#overview)

greatings, Manuela