View Full Version : display filename in a dir & auto fill it as value an array

01-10-2010, 05:39 PM
Hi All, nice to meet you :)

i Looking for some help here,

I have config.file where is as file configuration to another file, and inside are:

$fn[0]["short"] = "file-name.txt"; $fn[0]["long"] = "file name";
$fn[1]["short"] = "1_file-name.txt"; $fn[1]["long"] = "1 file name";

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

I was try to handle it with atomically insert using small php code, but i not sure it's done :confused:, because it just echoing and than how to split it?

here the e.g. code:

$path = ".";
$dh = opendir($path);
$i= 0;
while (($file = readdir($dh)) !== false) {
if($file != "." && $file != "..") {
echo "\$fn[$i]['short'] = '$file'; $fn[$i]['long'] = '$file(splited)';<br />"; // Test
} closedir($dh);
because when this code include to config.php file won't work properly
so Every idea would be welcome and could be help me, Thank You :thumbsup:

01-10-2010, 08:50 PM
echo "\$fn[$i]['short'] = '$file'; \$fn[$i]['long'] = '$file(splited)';<br />";

I'm not too sure what you're trying to do, but I think you may want to escape the $ signs that you don't want to register as a variable. i.e., you want it to echo "$fn[1]['short] = 'file_name';..." and etc.

To auto inc. an array, simply do the following:

$fn = array();
$fn[] = 'one.file'; //$fn[0];
$fn[] = 'one.file'; //$fn[1];
//and so on...

For the second question, I'm unsure what you are asking for but if you want a script that will search through a directory and return all files, then you could easily find a script like this on the internet.

Your final question will require a basic string manipulation script (str_replace, or something similar).

01-11-2010, 06:29 AM
Hi Rebbu, you gave me very nice input...

in fact i just need to auto 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 > Value 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

so i don't want to type file name from directory one by one (manual) anymore & avoid some mistyping (that's my goal) with config.php

$fn[0]["short"] = "file-name.txt"; $fn[0]["long"] = "file name";
$fn[1]["short"] = "1_file-name.txt"; $fn[1]["long"] = "1 file name";

Using this script:

echo "\$fn[$i]['short'] = '$file'; \$fn[$i]['long'] = '$file(splited)';<br />";
?> because i think it can handling to auto populate value of config.php, but i'm wrong because it's just echoing, that's why i make question here just to looking for another better way.

Your final question will require a basic string manipulation script (str_replace, or something similar)this something stranger to me, can you explain it more please, that maybe can help me.. :) Many Thank you for your input.