basically i have created a selection box which display the files i have got in a certain folder which are xml files, my problem is that i am unsure how to save the content of the file selected as a live variable which changes on different selected which the variable can be called to show a table.

here my code so far;

<?php

$dir = opendir("C:/xampp/htdocs/xml");

//List files in images directory
while (($file = readdir($dir)) !== false)
  {
      $last4 = substr($file, -4); 
      if ($last4 == ".xml")
      {
         $files[] = $file;

      }
  }
closedir($dir);

printf ('<select>');

foreach($files as $let => $val){
    printf ('<option value="'.$let.'">'.$val.'</option>');

}
printf ('</select>');

?>

Recommended Answers

All 2 Replies

Hi,

Just need patience in waiting for the response. All expert and master coders are volunteers, and ONE junior volunteer like myself is always lazy all the time :)..

In fact, I think I already posted many variations of read_dir function in this forum.. Since, I don't keep a record of those little things, let me write another one.. always new every time..

here we go... I don't have the chance to test my codes below, but I am pretty sure it will work..

   <?php

        function xmlFiles($dir,$ext) {
        $d = dir($dir); 
        while (false!== ($file = $d->read())) 
        {
        $extension = substr($file, strrpos($file, '.')); 
        if(($extension == '.'. $ext))
        $xml[$file] = $file; 
        }
        $d->close(); 
        asort($xml); 

        return $xml; 
        }

to use the function above, do it like this

        $theseXml = xmlFiles('./xml/','xml');
        echo '<select>';
        foreach($theseXml as $file){
        echo '<option value="'.$file.'">'. $file .'</option>';

        }

        echo '</select>';

Always refer to directories in reference with the htdocs directory. Everything within this directory are viewable through a browser. So, this C:/xampp/htdocs/xml is not valid if you are using a PHP script located inside the htdocs directory.

If you want to use something similar to that, then you will have to define it like this.

    define('DS', DIRECTORY_SEPARATOR);
    define('ROOT', dirname(__FILE__));

and your xml directory will can be referenced like this.. assuming that it is located in the htdocs or root directory.

    $xmlDir = ROOT.DS .'xml'. DS;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.