如何从文件夹中数组文件'


How to array file's name from folder?

我想从'image'文件夹创建自动补全。我有超过50张图片在"图像"文件夹。如何从文件夹数组所有的图像名称?

search.php

<?php
/*
 * Load sample data
 */
include 'data.php';
/*
 * Results array
 */
$results = array();
/*
 * Autocomplete formatter
 */
function autocomplete_format($results) {
    foreach ($results as $result) {
        echo $result[0] . '|' . $result[1] . "'n";
    }
}
/*
 * Search for term if it is given
 */
if (isset($_GET['q'])) {
    $q = strtolower($_GET['q']);
    if ($q) {
        foreach ($data as $key => $value) {
            if (strpos(strtolower($key), $q) !== false) {
                $results[] = array($key, $value);
            }
        }
    }
}
/*
 * Output format
 */
$output = 'autocomplete';
if (isset($_GET['output'])) {
    $output = strtolower($_GET['output']);
}
/*
 * Output results
 */
if ($output === 'json') {
    echo json_encode($results);
} else {
    echo autocomplete_format($results);
}

data.php

    <?php
        $data = array( /*
 * image_name
 */ );
html

<link rel="stylesheet" type="text/css" href="src/jquery.autocomplete.css">
    <script type="text/javascript" src="src/jquery.autocomplete.js"></script>
<script type="text/javascript" src="src/jquery.min.js"></script>
    <script>
     $("#ac5").autocomplete('search.php', {
            minChars: 1,
            useDelimiter: true,
            selectFirst: true,
            autoFill: true,
        });
    </script>
    <form>
            <input type="text" id="ac5">
        </form>

试一下

我已经取了数据库文件夹,在这个文件夹中有4个文件,所以打印的文件名为数组。

代码:

<?php
$dir    = '/work/database';
$files1 = scandir($dir);
print_r($files1);
?>
输出

: -

  Array ( [0] => . [1] => .. [2] => logo.png [3] => logos.jpg [4] => nbc_logo.jpg [5] => sciit_logo.png ) 

根据您的问题,我理解您想要在特定文件夹中获得所有图像列表的最佳方法是在php中使用glob()函数。

$images = glob('/youfolderpath/*.{jpeg,gif,png}', GLOB_BRACE);
//images has all your image names will jpeg, gif and png format, $images is an array that contains all your image names

有用链接:http://www.w3schools.com/php/func_filesystem_glob.asp

可以使用scandir()函数。更多链接:http://php.net/manual/en/function.scandir.php