简体   繁体   中英

Get a list of files and filenames without an extention using php

I'm trying to make a jQuery slider that automatically loads all the images in a specified folder. So I'm using a small PHP script that makes a list of all the files in that directory. For the captions of the slider I wanted to use the filename (without extension).

I'm using the following script, using PHP. It can list all the files with the extensions, but I can't find a way to also display the filename (for the captions) without the extensions.

Anyone an idea? Thanks in advance!

        <? 
    $path = "img"; 
    $dir_handle = @opendir($path) or die("Unable to open $path"); 
    while ($file = readdir($dir_handle)) { 
    if($file == "." || $file == ".." || $file == "index.php" ) 
    continue; 
    echo "$file"; 
    } 
    closedir($dir_handle); 
    ?>

Here you have more OO way:

$iterator = new DirectoryIterator($directory);
    foreach ($iterator as $fileinfo) {
        if ($fileinfo->isFile()) {
            $fileinfo->getBasename('.' .$fileinfo->getExtension());
        }
    }

您可以使用pathinfo()获取不带扩展名的文件名:

$filename = pathinfo( $file, PATHINFO_FILENAME);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM