Jump to content

php include files from a folder


pyrosarco

Recommended Posts

I want to make a folder filled with blog posts by date.  Each file will be like 20071113.php and 20071031.php basically the date they were written 2007, 10 for october, and 31 for the day.  But each will be notated like that so that the number is always increasing. 

 

How would I make a php include function as to take the last ten highest numbers and add it to a page? (Basically the last ten posts by date) and  How would I include more than one post per date?

Link to comment
https://forums.phpfreaks.com/topic/77179-php-include-files-from-a-folder/
Share on other sites

Well, very simply, something like this might suffice:

 

<?php
$files = array();
$path_to_folder = 'path/to/your/folder/of/blogs/';//where is this folder?
foreach(glob($path_to_folder.'*.php') as $file){//cycle through each .php file in that folder - i assume it ONLY contains blogs?
$files[] = $file;
}
rsort($files);//sort our files
foreach($files as $file){
include($file);
echo '<br />';
}
?>

 

Given the consistant naming format, sorting by the file name will work fine to sort them by date/

 

I'm assuming you do want to include the files, since you said they are .php files - rather than plain text files which you'll want to grab with file_get_contents() and echo.

 

Personally, i think you're far better off using a database for storing your blogs though.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.