?
Solved

display from directory

Posted on 2008-06-24
5
Medium Priority
?
176 Views
Last Modified: 2010-04-06
Hi,

I need to know how I can get last ten file in the directory using PHP ?? Thanks
0
Comment
Question by:usmbay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:Neoific
ID: 21856557
How do you want them to be sorted?
0
 
LVL 1

Expert Comment

by:morristhebear
ID: 21858664
I've just written this for you, hopefully it will do the trick.  It's a function that returns the last 10 files modified in a given directory.  It has an optional parameter so you can specify how many files to return, if left blank, it defaults to 10.  The files are returned sorted in descending order.  You could list the files using a foreach loop.

E.G:

$files = list_files ("C:\\");
foreach ($files as $k => $v) {
    echo "$v<br />\n";
}

Hope this helps.

More Example Usage:

print_r ( list_files ("C:\\"));    //  Lists the last 10 modified files in the C:\ drive.
print_r ( list_files ("C:\\Program Files\\", 20));   // Lists the last 20 modified files in Program Files


<?php
	
	function list_files ($string_dir, $int_limit = 10) {
		$files = array();
		//	Get all the files in the directory, along with the last modified date.
		if ($dir_handle = opendir ($string_dir)) {
			while (false !== ($file = readdir ($dir_handle))) {
				$files[date("YmdHis", filemtime($string_dir . $file))] = $file;
	    	}
	    	krsort ($files);
	    	$files = array_chunk ($files, $int_limit, true);
	    	return $files[0];
		}
		return false;
	}
?>

Open in new window

0
 

Author Comment

by:usmbay
ID: 21860178
in this query the file_name represent the file name but the problem is the pdf_files includes all the files name either exist or not so when I excute the query it return all files name but  they're not physically exist
so I need to return file_name physically exist only in the directory

SELECT      file_name  FROM pdf_files;

file_name from the database
101
102
103
104
105

but what's in the directory is till 103 only so in the query return only up to 103.pdf  

Thanks
0
 
LVL 1

Accepted Solution

by:
morristhebear earned 1500 total points
ID: 21860289
OK.  You can use the file_exists function with a loop and break out of it once you find a file that isn't found.  The snippet below should send you in the right direction, you will need to change the file_exists part where it says path to point to your directory where the PDF files are stored.

Hope this helps.
<?php
	
	$query = "SELECT file_name FROM pdf_files";
	while ($row = mysql_fetch_array($query)) {
		if (file_exists('path' . $row['file_name'] . '.pdf')) {
			echo $row['file_name'] . "<br />\n";
		} else {
			break;
		}
	}
	
?>

Open in new window

0
 

Author Comment

by:usmbay
ID: 22000811
ok it works but it return the files from last year it's log list , is there a way to return last 10 file only
Thanks
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question