Search code examples
phparraysfilenamesfill

auto display filename in a directory and auto fill it as value


I want something (final) like this :

<?php
//named as config.php
$fn[0]["long"] = "file name";   $fn[0]["short"] = "file-name.txt";  
$fn[1]["long"] = "file name 1"; $fn[1]["short"] = "file-name_1.txt";
?>

What that I want to?:

1. $fn[0], $fn[1], etc.., as auto increasing
2. "file-name.txt", "file-name_1.txt", etc.., as file name from a directory, i want it auto insert.
3. "file name", "file name 1", etc.., is auto split from "file-name.txt", "file-name_1.txt", etc..,

and config.php above needed in another file e.g.

<? //named as form.php
include "config.php";
for($tint = 0;isset($text_index[$tint]);$tint++)
{
if($allok === TRUE && $tint === $index) echo("<option VALUE=\"" . $text_index[$tint]["short"] . "\" SELECTED>" . $text_index[$tint]["long"] . "</option>\n");
else echo("<option VALUE=\"" . $text_index[$tint]["short"] . "\">" . $text_index[$tint]["long"] . "</option>\n");
} ?>

so i try to search and put php code and hope it can handling at all : e.g.

<?php
$path = ".";
$dh = opendir($path);
//$i=0; 
$i= 1;
while (($file = readdir($dh)) !== false) {
if($file != "." && $file != "..") {
echo "\$fn[$i]['short'] = '$file'; $fn[$i]['long'] = '$file(splited)';<br />"; // Test 
       $i++;
}
}
closedir($dh);
?>

but i'm wrong, the output is not similar to what i want, e.g.

$fn[0]['short'] = 'file-name.txt'; ['long'] = 'file-name.txt'; //<--not splitted
$fn[1]['short'] = 'file-name_1.txt'; ['long'] = 'file-name_1.txt'; //<--not splitted

because i am little known with php so i don't know how to improve code more, there are any good tips of you guys could help me, Please


Solution

  • New answer after OP edited his question

    From your edited question, I understand you want to dynamically populate a SelectBox element on an HTML webpage with the files found in a certain directory for option value. The values are supposed to be split by dash, underscore and number to provide the option name, e.g.

    Directory with Files    >    SelectBox Options
    filename1.txt           >    value: filename1.txt, text: Filename 1
    file_name2.txt          >    value: filename1.txt, text: File Name 2
    file-name3.txt          >    value: filename1.txt, text: File Name 3
    

    Based from the code I gave in my other answer, you could achieve this with the DirectoryIterator like this:

    $config = array();
    $dir    = new DirectoryIterator('.');
    foreach($dir as $item) {
        if($item->isFile()) {
            $fileName = $item->getFilename();
            // turn dashes and underscores to spaces
            $longFileName = str_replace(array('-', '_'), ' ', $fileName);
            // prefix numbers with space
            $longFileName = preg_replace('/(\d+)/', ' $1', $fileName);
            // add to array
            $config[] = array('short' => $filename,
                              'long'  => $longFilename);
        }
    } 
    

    However, since filenames in a directory are unique, you could also use this as an array:

    $config[$filename] => $longFilename;
    

    when building the config array. The short filename will form the key of the array then and then you can build your selectbox like this:

    foreach($config as $short => $long)
    {
        printf( '<option value="%s">%s</option>' , $short, $long);
    }
    

    Alternatively, use the Iterator to just create an array of filenames and do the conversion to long file names when creating the Selectbox options, e.g. in the foreach loop above. In fact, you could build the entire SelectBox right from the iterator instead of building the array first, e.g.

    $dir = new DirectoryIterator('.');
    foreach($dir as $item) {
        if($item->isFile()) {
            $fileName = $item->getFilename();
            $longFileName = str_replace(array('-', '_'), ' ', $fileName);
            $longFileName = preg_replace('/(\d+)/', ' $1', $fileName);
            printf( '<option value="%s">%s</option>' , $fileName, $longFileName);
        }
    } 
    

    Hope that's what your're looking for. I strongly suggest having a look at the chapter titled Language Reference in the PHP Manual if you got no or very little experience with PHP so far. There is also a free online book at http://www.tuxradar.com/practicalphp