PHP List all files in directory  | 
	 
	 
PHP List all files in directory  | 
	 
	 
			
			  Aug 28 2010, 10:51 AM
			
				 Post
					#1
					
				
			 
		 | 
	|
        	
				
					![]() Mel Blanc was allergic to carrots. ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Designer Posts: 6,371 Joined: Aug 2008 Member No: 676,291  | 
       
			
			 
				I was working on a little file upload thing for a website and I'm pretty much done. However, I have a page where I list all files based on the current user. My current script works fine, but it only lists the most recent file uploaded. I'd rather have a full list of all files in the directory instead of the most recent one. 
			
			
					
		Here's my current script: CODE <?php $directory = $user; if (!is_dir($directory)) { mkdir($directory); $list = '<tr> <td>No files to display.</td> </tr>'; } else { if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { $name = ucfirst($file); $list = '<tr> <td>' . $name . '</td> <td><a href="/files/' . $directory . '/?delete=' . $file . '" class="delete">Delete this file</a> <a href="/files/' . $directory . '/?open=' . $file . '">Open this file</a></td> </tr>'; } else { $list = '<tr> <td>No files to display.</td> </tr>'; } } closedir($handle); } echo $list; } ?> It works exactly like I want it to when it comes to listing, but I want to make it so it lists all files as opposed to one. Oh, and, $user is just variable I defined in another script that contains the current user's username. Hilfe?  | 
	
| 
			
			 | 
	|
![]()  | 
	
			
			  Aug 28 2010, 06:38 PM
			
				 Post
					#2
					
				
			 
		 | 
	|
        	
				
					![]() Member ![]() ![]() Group: Member Posts: 22 Joined: Dec 2008 Member No: 702,109  | 
       
			
			 CODE while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { $name = ucfirst($file); $list = '<tr> <td>' . $name . '</td> <td><a href="/files/' . $directory . '/?delete=' . $file . '" class="delete">Delete this file</a> <a href="/files/' . $directory . '/?open=' . $file . '">Open this file</a></td> </tr>'; } else { $list = '<tr> <td>No files to display.</td> </tr>'; } } IF you look at this part of your code is where your error lies. The WHILE LOOP iterate over the $file which is an array... and you are store the data in a variable which is not an array $list, so it just keeps overwriting the file name with a new one every time. CODE <?php 
				
				
				
			$directory = $user; $list = array(); if (!is_dir($directory)) { mkdir($directory); $list = '<tr> <td>No files to display.</td> </tr>'; } else { if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { if( $file != "." && $file != ".." ) $list[] .= $file; } } else { $list = '<tr> <td>No files to display.</td> </tr>'; } } closedir($handle); foreach ( $list as $key => $val ) { echo "<tr><td> {$val} </td></tr>"; } ?>  | 
	
| 
			
			 | 
	|
			
			  Aug 29 2010, 05:19 AM
			
				 Post
					#3
					
				
			 
		 | 
	|
        	
				
					![]() Mel Blanc was allergic to carrots. ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Designer Posts: 6,371 Joined: Aug 2008 Member No: 676,291  | 
       
			
			 
				Oh, okay. Thanks man, worked great!
				
				
				
			 
			
			
					
		 | 
	
| 
			
			 | 
	|
			
			  Aug 30 2010, 04:06 PM
			
				 Post
					#4
					
				
			 
		 | 
	|
        	
				
					![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 8,629 Joined: Jan 2007 Member No: 498,468  | 
       
			
			 
				Topic Closed and Moved
				
				
				
			 
			
			
					
		 | 
	
| 
			
			 | 
	|
![]() ![]()  |