Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 571915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:33:29+00:00 2026-05-13T13:33:29+00:00

I want something (final) like this : <?php //named as config.php $fn[0][long] = file

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:33:29+00:00Added an answer on May 13, 2026 at 1:33 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 283k
  • Answers 284k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check out http://piratepad.net and http://ietherpad.com And you can embed those… May 13, 2026 at 4:23 pm
  • Editorial Team
    Editorial Team added an answer Figured it out... I'm using regex to serve only my… May 13, 2026 at 4:23 pm
  • Editorial Team
    Editorial Team added an answer The one you haven't tried is DataContractSerializer. There is a… May 13, 2026 at 4:23 pm

Related Questions

Within my php app I have multiple templates that get combined to form a
I'm pulling 500 results from a search query to a webservice; I store these
Usage scenario We have implemented a webservice that our web frontend developers use (via
How do I get the collection of errors in a view? I don't want
Recently I've been doing a lot of these enum Thing { /* etc etc

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.