Jump to content

[SOLVED] The best way to dynamically know what files are located on a server


jsims281forum

Recommended Posts

Hi I'm currently building a basic CMS which should be able to drop into any basic flat file site and give my users a WYSIWYG interface to edit their pages. I'm using FCKED for the editing functions, and I've got it running nicely, but only if you can hard code the file names into the system, using a simple if statement e.g.

 

if ($_GET["pageid"] == "1" ) 
{
	$content = file_get_contents("../index.htm"); //set this to point to the page you want to edit
}

 

 

I'm wondering if anyone can tell me where to start if I want to have the system work out all the file names on the server, i.e. to incorporate a (customisable) file browser that will work on the server. I want the user to log into the system and get a list of all files in their public html directory, so they can click one and load it up in fckedior.

 

I'm currently looking into modifying webfilebrowser, available at http://sourceforge.net/projects/webfilebrowser/.

 

If anyone has any suggestions on how I could achieve this, or even just link to a good basic open source file browser I would be really appreciative.

 

Thanks,

James

 

 

Maybe it's worth looking at the problem from a different angle? Wouldn't it be better if the pages were stored in a database?

 

If you want to fetch the contents of all your html files, you could use a foreach loop and the glob() function.

Yeah using a database would make my life a lot easier, however this is something that should be able to be retro-fitted to an existing website with the minimum of fuss. I could make a script to automatically load all the files into a database but that would lead to the same issue I think - how to get the system aware of what files are on the server.

 

What I'm really after is a folder that can be uploaded e.g. /cms, and works straight "out of the box" without having to convert the users' site.

 

I've found this bit of code which I think could help, I'll post back if I can get it working.

 

 <?php  

// initialize counter
$count = 0;
  

// set directory name
$dir = "/bin";
  

// open directory and parse file list
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
// iterate over file list
// print filenames
while (($filename = readdir($dh)) !== false)
{
if (($filename != ".") && ($filename != ".."))
{
$count ++;
echo $dir . "/" . $filename . "\n";
}
}
// close directory
closedir($dh);
}
}
  

echo "-- $count FILES FOUND --";
  

?>

 

I think I might be able to work with this so it populates an array with each file name, then pulls those array values out again to work on?

Ok, just so anyone else is trying to do the same, the code I've come up with for managing file names is this:

 

$pages = array();

foreach (glob("*.html") as $filename) 
{
    	//echo "$filename size " . filesize($filename) . "\n<br>";
array_push ($pages,$filename);
}
foreach ($pages as &$value) {
    echo "<a href=",$value,">",$value,"</a><br/>";
}

Why use an array and iterate one other time when you can print the value directly:

 

<?php
foreach(glob('*.html') as $file){
     $filename = basename($file);
     echo '<a href="' . $file . '">' . $filename . '</a>;
}
?>

Good point, that also works.

 

I like having an array there so I can just call $pages every time I want a file name. Also it wil let me sort the names before they get echo'd, and other funky stuff. I also want to be able to call the list of filenames in various places and ways the system.

 

However I'm no guru, so I might be missing the point. Is using an array pointless even when I want to do things like this?

It makes sense having an array if you're printing the files in more then one place. For the sorting thing, glob() should automatically sort the files if the  GLOB_NOSORT parameter isn't set. Each way is mostly the same, so don't bother too much.

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.