List files from a PHP folder / directory of certain extensions

Asked

Viewed 28,766 times

11

I need to list the files of a folder, and display them by name linked to his directory for download.

I use that code:

$pasta = 'uploads/';
$arquivos = glob("$pasta{*.jpg,*.JPG,*.png,*.gif,*.bmp}", GLOB_BRACE);
foreach($arquivos as $img){
   echo $img;
}

So far so good. but it displays the directory and the complete file like this:

uploads/editail1.jpg

and I wanted him to display only the file name.

  • 3

    Possible duplicate of Folder browsing in PHP

  • 1

    @Danielomine That is recursive, facing directory trees and not simple folders. I thought it was important to have a specific and objective answer for folders. I imagine it’s much easier to adapt this one to a user who needs a simple solution than someone understanding what to remove from it. IN TIME: I don’t think you voted wrong, I just understand that the problem is different enough to be separate questions. But whatever the community decides.

1 answer

17


Using your code:

chdir( 'pasta_desejada' );
$arquivos = glob("{*.png,*.jpg,*.jpeg,*.bmp,*.gif}", GLOB_BRACE);
foreach($arquivos as $img) echo $img;

Using the standard functions for PHP directories:

$types = array( 'png', 'jpg', 'jpeg', 'gif' );
if ( $handle = opendir('pasta_desejada') ) {
    while ( $entry = readdir( $handle ) ) {
        $ext = strtolower( pathinfo( $entry, PATHINFO_EXTENSION) );
        if( in_array( $ext, $types ) ) echo $entry;
    }
    closedir($handle);
}    

You have this possibility too:

$types = array( 'png', 'jpg', 'jpeg', 'gif' );
$path = 'pasta_desejada';
$dir = new DirectoryIterator($path);
foreach ($dir as $fileInfo) {
    $ext = strtolower( $fileInfo->getExtension() );
    if( in_array( $ext, $types ) ) echo $fileInfo->getFilename();
}

See the 3 code snippets working on IDEONE.


Notes:

  • In PHP < 5.3.6, the 3rd example needs to be changed:

    $ext = strtolower( pathinfo( $fileInfo->getFilename(), PATHINFO_EXTENSION) );
    
  • In the case of the 2nd and 3rd examples, do not put the same extension in upper and lower case. Only lower case, because the strtolower is already normalizing file extensions.

  • It worked fine, only that I wanted you to assist me in one more detail, I inked the file to download. only it gives error when the file has type special characters. (PREG O%20PRESENCIAL.jpg). how do I solve? I have already applied the conversion to UTF-8. changed, but did not resolve.

  • This question here talks about it, I think I’ve seen others with this subject also http://answall.com/questions/15723 - But the truth is one: it will always give a lot of trouble. The right thing to do is always record without an accent and everything in tiny. It takes a little extra work to implement, but it’s much simpler than having to tidy up all the time. Each hosting and OS behaves in a way, there is no good solution with accents in the file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.