Ok i am pretty new to php so i could be missing a simple trick here. I need to be able to read all text files within a given directory and have them appear in a combo box by file name. After searching for what seems like forever, i can only seem to find methods of this using sql. Is this possible without the use of databases?

thanks in advance

try this out

<?php
//specify path of directory
$dir = 'text/';

//open directory
if ($handle = opendir($dir)) 
{
    
	//read each file
	while ($file = readdir($handle)) {
		//only get files of a particular type       
	    if(mime_content_type($dir . $file) == 'text/plain')
		{		
			//eliminate additional directories
			if ($file != "." && $file != "..")
			{				
				//store filename in array
				$option[] = $file ; 
			}			
		}
    }

	//close directory
    closedir($handle);
	
	//create combo box
	$cbo = '<select name="select" id="select">';
	
	//create option for each file
	foreach($option as $key=>$val)
	{
		$cbo .='<option value="'.$val.'">'.$val.'</option>';
	}
	
	//close combo box
	$cbo .= '</select>';
	
	//output combo box (might want to put this in a form tag on your final page)
	echo $cbo;
	
}

?>

sorry, the indentation got a little messed up on paste

works perfectly, just what the doctor ordered. Thanks a lot for that very much appreciated.

always glad when a snippet in the toolbox can be of use to others!

try this out

<?php
//specify path of directory
$dir = 'text/';

//open directory
if ($handle = opendir($dir)) 
{
    
	//read each file
	while ($file = readdir($handle)) {
		//only get files of a particular type       
	    if(mime_content_type($dir . $file) == 'text/plain')
		{		
			//eliminate additional directories
			if ($file != "." && $file != "..")
			{				
				//store filename in array
				$option[] = $file ; 
			}			
		}
    }

	//close directory
    closedir($handle);
	
	//create combo box
	$cbo = '<select name="select" id="select">';
	
	//create option for each file
	foreach($option as $key=>$val)
	{
		$cbo .='<option value="'.$val.'">'.$val.'</option>';
	}
	
	//close combo box
	$cbo .= '</select>';
	
	//output combo box (might want to put this in a form tag on your final page)
	echo $cbo;
	
}

?>

i tried this code but i have an exception that says
" Object of class stdClass could not be converted to string "
in this line-->$cbo .='<option value="'.$val.'">'.$val.'</option>';
so,how i can solve this

Please show your full code with explanation.

Please show your full code with explanation.

hii ayesha789,
i just tried to select column from table and load the return value in combo box here is the code:-

$query = "SELECT url FROM " .
     "{managelinks }";

  $queryResult =  db_query($query);


  $block_content = '';  
  $cbo ='';
 
  while ($links = db_fetch_object($queryResult)) 
  {   
   $option[] = $links ;
  }
  
  $cbo .= '<select name="select" id="select">'; 	
  
  foreach( $option as $key => $val)	
  {		
  $cbo .=(String)"<option>".$val."</option>";	
  
  } 	
  
  
  $cbo .= '</select>';

but i found this exception:-
"recoverable fatal error: Object of class stdClass could not be converted to string in "

so any help to solve this exception.

thanks for advance

Sorry for dragging up an ancient thread, not usualy one for doing that.

Since i have been dabbling in php again now i have time i tweaked the original code snippet posted in this thread to display files by the date they were created (newest first), but ended up with erratic results.

// find all files in directory				 
	$uploadDir = 'video_entries/';      
            if ($handle = opendir($uploadDir)) { 
              while (false !== ($file = readdir($handle))) {       
                if ($file != "." && $file != ".." && $file != "addvideo.php") { 
                    $file_arr[] = $file; 
                } 
              } 
            closedir($handle); 
            }		
						
 

function DateCmp($a, $b)
            {
              return ($a[1] < $b[1]) ? -1 : 0;
            }

function SortByDate(&$file_arr)
            {
            usort($file_arr, 'DateCmp');
            }

// sort file array by time created            
SortByDate($file_arr);

I then use a foreach loop to display all the files, as shown below. (sorry about the exess html code).

foreach ($file_arr as $file) {

echo "<tr><td class=\"header_video\">";
echo "&nbsp";
echo "</tr></td>";
								
echo "<tr><td class=\"entrycontent_video\" valign=\"top\" align=\"left\">"; 
include("video_entries/$file");
echo "</td></tr>";

echo "<td class=\"video_spacer\"></td>";
}

Now the page displays correctly as intended and there are no php errors or warnings. The only issue is that the files dont display in the order i want ie by date. which is the sole purpose of the script.

I am guessing that the error, a logical one, is somewhere in either the DateCmp or SortByDate function, but cant seem to get my head around it.

Any help on this one would be a life saver!

#
//output combo box (might want to put this in a form tag on your final page)
#
echo $cbo;

How would I add this in a form tag on my final page?

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.