如何从文件夹中排列文件的名称?

时间:2015-05-27 10:20:51

标签: php arrays

我想从'image'文件夹创建自动填充功能。我在'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>

我使用jquery.autocomplete.js而不工作......

3 个答案:

答案 0 :(得分:0)

根据你的问题,我理解你想要特定文件夹中所有图像的名称,以获得目录中所有图像的最佳方式是在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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

尝试这样。

我已经拿了数据库文件夹,在这个文件夹中有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 )