Directory Listing in PHP












-2















I have a php function that allows me to delete images from a certain directory. Now my problem is that in my code, i can see the index.php file listed, and i only want to show the images under it. Here is my full code:



$fid= $_POST['fid'];
if (("submit")&&($fid != "")) {
foreach($fid as $rfn) {
$remove = "$dir/$rfn";
unlink($remove);
}
}
$handle=opendir($dir);
while (($file = readdir($handle))!== false){
if ($file != "." && $file != "..") {
$size = filesize("$dir/$file");
$list .= '<div class="col-md-3 text-center" style="margin-top:20px;">';
$list .= '<img src="../inc/img/galeria/'.$file.'" class="rounded" width="100%" height="250px">';
$list .= '<br><br>';
$list .= '<input type="checkbox" class="form-control" name="fid" value="'.$file.'">';
$list .= '</div>';
}
}
closedir($handle);
echo $list;


Now this code works just fine, the problem is it lists everything inside the directory and i want to show only the jpg, jpeg, gif or png files inside of that directory. Thanks in advance guys.










share|improve this question























  • what is this first if features ("submit") string for?

    – Marcin Orlowski
    Nov 24 '18 at 0:55











  • I use it to submit a form to delete the image that is listed

    – Pedro Carvalho
    Nov 24 '18 at 0:57






  • 1





    if ("submit") makes no sense. It's always TRUE

    – Marcin Orlowski
    Nov 24 '18 at 0:59











  • You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?

    – Pedro Carvalho
    Nov 24 '18 at 1:02






  • 1





    stackoverflow.com/questions/3226519/…

    – Pirate of Marmara
    Nov 24 '18 at 1:05
















-2















I have a php function that allows me to delete images from a certain directory. Now my problem is that in my code, i can see the index.php file listed, and i only want to show the images under it. Here is my full code:



$fid= $_POST['fid'];
if (("submit")&&($fid != "")) {
foreach($fid as $rfn) {
$remove = "$dir/$rfn";
unlink($remove);
}
}
$handle=opendir($dir);
while (($file = readdir($handle))!== false){
if ($file != "." && $file != "..") {
$size = filesize("$dir/$file");
$list .= '<div class="col-md-3 text-center" style="margin-top:20px;">';
$list .= '<img src="../inc/img/galeria/'.$file.'" class="rounded" width="100%" height="250px">';
$list .= '<br><br>';
$list .= '<input type="checkbox" class="form-control" name="fid" value="'.$file.'">';
$list .= '</div>';
}
}
closedir($handle);
echo $list;


Now this code works just fine, the problem is it lists everything inside the directory and i want to show only the jpg, jpeg, gif or png files inside of that directory. Thanks in advance guys.










share|improve this question























  • what is this first if features ("submit") string for?

    – Marcin Orlowski
    Nov 24 '18 at 0:55











  • I use it to submit a form to delete the image that is listed

    – Pedro Carvalho
    Nov 24 '18 at 0:57






  • 1





    if ("submit") makes no sense. It's always TRUE

    – Marcin Orlowski
    Nov 24 '18 at 0:59











  • You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?

    – Pedro Carvalho
    Nov 24 '18 at 1:02






  • 1





    stackoverflow.com/questions/3226519/…

    – Pirate of Marmara
    Nov 24 '18 at 1:05














-2












-2








-2








I have a php function that allows me to delete images from a certain directory. Now my problem is that in my code, i can see the index.php file listed, and i only want to show the images under it. Here is my full code:



$fid= $_POST['fid'];
if (("submit")&&($fid != "")) {
foreach($fid as $rfn) {
$remove = "$dir/$rfn";
unlink($remove);
}
}
$handle=opendir($dir);
while (($file = readdir($handle))!== false){
if ($file != "." && $file != "..") {
$size = filesize("$dir/$file");
$list .= '<div class="col-md-3 text-center" style="margin-top:20px;">';
$list .= '<img src="../inc/img/galeria/'.$file.'" class="rounded" width="100%" height="250px">';
$list .= '<br><br>';
$list .= '<input type="checkbox" class="form-control" name="fid" value="'.$file.'">';
$list .= '</div>';
}
}
closedir($handle);
echo $list;


Now this code works just fine, the problem is it lists everything inside the directory and i want to show only the jpg, jpeg, gif or png files inside of that directory. Thanks in advance guys.










share|improve this question














I have a php function that allows me to delete images from a certain directory. Now my problem is that in my code, i can see the index.php file listed, and i only want to show the images under it. Here is my full code:



$fid= $_POST['fid'];
if (("submit")&&($fid != "")) {
foreach($fid as $rfn) {
$remove = "$dir/$rfn";
unlink($remove);
}
}
$handle=opendir($dir);
while (($file = readdir($handle))!== false){
if ($file != "." && $file != "..") {
$size = filesize("$dir/$file");
$list .= '<div class="col-md-3 text-center" style="margin-top:20px;">';
$list .= '<img src="../inc/img/galeria/'.$file.'" class="rounded" width="100%" height="250px">';
$list .= '<br><br>';
$list .= '<input type="checkbox" class="form-control" name="fid" value="'.$file.'">';
$list .= '</div>';
}
}
closedir($handle);
echo $list;


Now this code works just fine, the problem is it lists everything inside the directory and i want to show only the jpg, jpeg, gif or png files inside of that directory. Thanks in advance guys.







php directory-listing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 0:52









Pedro CarvalhoPedro Carvalho

488




488













  • what is this first if features ("submit") string for?

    – Marcin Orlowski
    Nov 24 '18 at 0:55











  • I use it to submit a form to delete the image that is listed

    – Pedro Carvalho
    Nov 24 '18 at 0:57






  • 1





    if ("submit") makes no sense. It's always TRUE

    – Marcin Orlowski
    Nov 24 '18 at 0:59











  • You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?

    – Pedro Carvalho
    Nov 24 '18 at 1:02






  • 1





    stackoverflow.com/questions/3226519/…

    – Pirate of Marmara
    Nov 24 '18 at 1:05



















  • what is this first if features ("submit") string for?

    – Marcin Orlowski
    Nov 24 '18 at 0:55











  • I use it to submit a form to delete the image that is listed

    – Pedro Carvalho
    Nov 24 '18 at 0:57






  • 1





    if ("submit") makes no sense. It's always TRUE

    – Marcin Orlowski
    Nov 24 '18 at 0:59











  • You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?

    – Pedro Carvalho
    Nov 24 '18 at 1:02






  • 1





    stackoverflow.com/questions/3226519/…

    – Pirate of Marmara
    Nov 24 '18 at 1:05

















what is this first if features ("submit") string for?

– Marcin Orlowski
Nov 24 '18 at 0:55





what is this first if features ("submit") string for?

– Marcin Orlowski
Nov 24 '18 at 0:55













I use it to submit a form to delete the image that is listed

– Pedro Carvalho
Nov 24 '18 at 0:57





I use it to submit a form to delete the image that is listed

– Pedro Carvalho
Nov 24 '18 at 0:57




1




1





if ("submit") makes no sense. It's always TRUE

– Marcin Orlowski
Nov 24 '18 at 0:59





if ("submit") makes no sense. It's always TRUE

– Marcin Orlowski
Nov 24 '18 at 0:59













You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?

– Pedro Carvalho
Nov 24 '18 at 1:02





You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?

– Pedro Carvalho
Nov 24 '18 at 1:02




1




1





stackoverflow.com/questions/3226519/…

– Pirate of Marmara
Nov 24 '18 at 1:05





stackoverflow.com/questions/3226519/…

– Pirate of Marmara
Nov 24 '18 at 1:05












3 Answers
3






active

oldest

votes


















0














This is how to scan a dir and only process certain files. Adapt for your use:



$handle=opendir($dir);
while ( ($file = readdir($handle)) !== false ) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ( in_array($file, ['.', '..']) || ! in_array($ext, ['jpeg', 'jpg', 'gif', 'png']) ) {
continue;
}
// Do something with file
}





share|improve this answer































    0














    This fixes the issue, thank you guys for the tips!



    $images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);


    And also:



    // image extensions
    $extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');

    // init result
    $result = array();

    // directory to scan
    $directory = new DirectoryIterator('/dir/to/scan/');

    // iterate
    foreach ($directory as $fileinfo) {
    // must be a file
    if ($fileinfo->isFile()) {
    // file extension
    $extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
    // check if extension match
    if (in_array($extension, $extensions)) {
    // add to result
    $result = $fileinfo->getFilename();
    }
    }
    }
    // print result
    print_r($result);


    This one is even better and i have managed to make it work, shouts to @pirateofmarmara to guide me on this.






    share|improve this answer































      0














      You can try the following. I hope there is no syntax error. I did not run it.



      foreach(glob($dir . "/*.{jpg,jpeg,png}", GLOB_BRACE) as $file) {
      // echo $file;
      }





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454286%2fdirectory-listing-in-php%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        This is how to scan a dir and only process certain files. Adapt for your use:



        $handle=opendir($dir);
        while ( ($file = readdir($handle)) !== false ) {
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        if ( in_array($file, ['.', '..']) || ! in_array($ext, ['jpeg', 'jpg', 'gif', 'png']) ) {
        continue;
        }
        // Do something with file
        }





        share|improve this answer




























          0














          This is how to scan a dir and only process certain files. Adapt for your use:



          $handle=opendir($dir);
          while ( ($file = readdir($handle)) !== false ) {
          $ext = pathinfo($file, PATHINFO_EXTENSION);
          if ( in_array($file, ['.', '..']) || ! in_array($ext, ['jpeg', 'jpg', 'gif', 'png']) ) {
          continue;
          }
          // Do something with file
          }





          share|improve this answer


























            0












            0








            0







            This is how to scan a dir and only process certain files. Adapt for your use:



            $handle=opendir($dir);
            while ( ($file = readdir($handle)) !== false ) {
            $ext = pathinfo($file, PATHINFO_EXTENSION);
            if ( in_array($file, ['.', '..']) || ! in_array($ext, ['jpeg', 'jpg', 'gif', 'png']) ) {
            continue;
            }
            // Do something with file
            }





            share|improve this answer













            This is how to scan a dir and only process certain files. Adapt for your use:



            $handle=opendir($dir);
            while ( ($file = readdir($handle)) !== false ) {
            $ext = pathinfo($file, PATHINFO_EXTENSION);
            if ( in_array($file, ['.', '..']) || ! in_array($ext, ['jpeg', 'jpg', 'gif', 'png']) ) {
            continue;
            }
            // Do something with file
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 1:10









            ryantxrryantxr

            2,6691520




            2,6691520

























                0














                This fixes the issue, thank you guys for the tips!



                $images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);


                And also:



                // image extensions
                $extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');

                // init result
                $result = array();

                // directory to scan
                $directory = new DirectoryIterator('/dir/to/scan/');

                // iterate
                foreach ($directory as $fileinfo) {
                // must be a file
                if ($fileinfo->isFile()) {
                // file extension
                $extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
                // check if extension match
                if (in_array($extension, $extensions)) {
                // add to result
                $result = $fileinfo->getFilename();
                }
                }
                }
                // print result
                print_r($result);


                This one is even better and i have managed to make it work, shouts to @pirateofmarmara to guide me on this.






                share|improve this answer




























                  0














                  This fixes the issue, thank you guys for the tips!



                  $images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);


                  And also:



                  // image extensions
                  $extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');

                  // init result
                  $result = array();

                  // directory to scan
                  $directory = new DirectoryIterator('/dir/to/scan/');

                  // iterate
                  foreach ($directory as $fileinfo) {
                  // must be a file
                  if ($fileinfo->isFile()) {
                  // file extension
                  $extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
                  // check if extension match
                  if (in_array($extension, $extensions)) {
                  // add to result
                  $result = $fileinfo->getFilename();
                  }
                  }
                  }
                  // print result
                  print_r($result);


                  This one is even better and i have managed to make it work, shouts to @pirateofmarmara to guide me on this.






                  share|improve this answer


























                    0












                    0








                    0







                    This fixes the issue, thank you guys for the tips!



                    $images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);


                    And also:



                    // image extensions
                    $extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');

                    // init result
                    $result = array();

                    // directory to scan
                    $directory = new DirectoryIterator('/dir/to/scan/');

                    // iterate
                    foreach ($directory as $fileinfo) {
                    // must be a file
                    if ($fileinfo->isFile()) {
                    // file extension
                    $extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
                    // check if extension match
                    if (in_array($extension, $extensions)) {
                    // add to result
                    $result = $fileinfo->getFilename();
                    }
                    }
                    }
                    // print result
                    print_r($result);


                    This one is even better and i have managed to make it work, shouts to @pirateofmarmara to guide me on this.






                    share|improve this answer













                    This fixes the issue, thank you guys for the tips!



                    $images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);


                    And also:



                    // image extensions
                    $extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');

                    // init result
                    $result = array();

                    // directory to scan
                    $directory = new DirectoryIterator('/dir/to/scan/');

                    // iterate
                    foreach ($directory as $fileinfo) {
                    // must be a file
                    if ($fileinfo->isFile()) {
                    // file extension
                    $extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
                    // check if extension match
                    if (in_array($extension, $extensions)) {
                    // add to result
                    $result = $fileinfo->getFilename();
                    }
                    }
                    }
                    // print result
                    print_r($result);


                    This one is even better and i have managed to make it work, shouts to @pirateofmarmara to guide me on this.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 24 '18 at 1:11









                    Pedro CarvalhoPedro Carvalho

                    488




                    488























                        0














                        You can try the following. I hope there is no syntax error. I did not run it.



                        foreach(glob($dir . "/*.{jpg,jpeg,png}", GLOB_BRACE) as $file) {
                        // echo $file;
                        }





                        share|improve this answer




























                          0














                          You can try the following. I hope there is no syntax error. I did not run it.



                          foreach(glob($dir . "/*.{jpg,jpeg,png}", GLOB_BRACE) as $file) {
                          // echo $file;
                          }





                          share|improve this answer


























                            0












                            0








                            0







                            You can try the following. I hope there is no syntax error. I did not run it.



                            foreach(glob($dir . "/*.{jpg,jpeg,png}", GLOB_BRACE) as $file) {
                            // echo $file;
                            }





                            share|improve this answer













                            You can try the following. I hope there is no syntax error. I did not run it.



                            foreach(glob($dir . "/*.{jpg,jpeg,png}", GLOB_BRACE) as $file) {
                            // echo $file;
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 24 '18 at 1:20









                            koalabruderkoalabruder

                            1,42552434




                            1,42552434






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454286%2fdirectory-listing-in-php%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Lallio

                                Unable to find Lightning Node

                                Futebolista