List Files Alphabetically Problem

Discussion in 'PHP' started by newzone, Apr 23, 2009.

  1. #1
    I'm trying to use this code to list the files alphabetically but for some reason there are listed both, the files from the folder "items"and "images". I want only the files from the folder "images" to be listed.

    <?php
    	 echo '<option value="null">Use url below</option>';
    	 $dir = opendir('../items/images/');
     		while(false !== ($file = readdir($dir))) {
     		if($file != "." && $file != ".." && $file != "Thumbs.db")
    			{
    			$fileList[] = trim($file);
    			}
    }
    sort ($fileList); // sort the file list in the array
    reset ($fileList); // go back to the top of the array
    while (list ($key, $val) = each ($fileList)) {
    
    		 		echo '<option value="'.$val.'">'.$val.'</option>';
     			
     		}
     		closedir($dir);
     	?> 
    PHP:
    To list the files from the "items" folder i have this code :

    
     <?php
    	 echo '<option value="null">Use url below</option>';
     		$dir = opendir('../items');
     		while(false !== ($file = readdir($dir))) {
     		if($file != "." && $file != ".." && $file != "Thumbs.db")
    			{
    			$fileList[] = trim($file);
    			}
    }
    sort ($fileList); // sort the file list in the array
    reset ($fileList); // go back to the top of the array
    while (list ($key, $val) = each ($fileList)) {
    
    		 		echo '<option value="'.$val.'">'.$val.'</option>';
     			
     		}
     		closedir($dir);
     	?> 
    
    PHP:
    If i remove this last code the first will work correctly, any help how to make them both to work ?
     
    newzone, Apr 23, 2009 IP
  2. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you are adding the items to the same array
    try ... unset($fileList); ... before the second bit of code or use a different array ...
     
    mallorcahp, Apr 23, 2009 IP
  3. newzone

    newzone Well-Known Member

    Messages:
    2,865
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    135
    Digital Goods:
    1
    #3
    Used a different array and is working, thank you very much. :)
     
    newzone, Apr 23, 2009 IP