• Welcome to WebsiteBaker Community Forum. Please log in or sign up.
Welcome, Guest
News
Support WebsiteBaker
Your donations will help to:
PayPal
  • Pay for our dedicated server
  • Pay for domain registration
  • and much more!
You can donate by clicking on the button below.

'scandir' and 'foreach' in code module

Started by netraam, September 21, 2023, 09:35:45 PM  ( Read 19,587 times )

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:

       
$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

    Gast

    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

    Quotescandir(........./modules/sponsors/sponsors4/): Failed to open directory: not implemented"

    scandir() needs a path like this

    $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

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




      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.
        LG Harald

        "Fange nie an, aufzuhören - höre nie auf, anzufangen." Marcus Tullius Cicero (106-43 v.Chr.)

        "Never begin to stop - never stop beginning." Marcus Tullius Cicero (106-43 BC)

        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.

          Gast

           (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

            DarkViper

            Quote from: netraam on September 23, 2023, 12:42:09 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)

            greatings, Manuela
              Der blaue Planet - er ist nicht unser Eigentum - wir haben ihn nur von unseren Nachkommen geliehen[br]
              [i][b][color=red]"We need education to cope with digitalization - and NOT the digitalization of education.!"[/color][/b][/i][br]
              [i]Tägliches Stoßgebet: [b]Oh Herr, wirf Hirn vom Himmel ![/i]

              \n\n