Click to See Complete Forum and Search --> : Indexing & Link Creation Using PHP
_LOBO_
11-23-2004, 09:39 AM
Indexing & Link Creation Using PHP
Thank you for stop here:
Does anyone know of a pre made PHP script that will index a directory
structure & then create links based on what it found?
I found something called SSLinks, which is basically what I'm looking for. Only problem is that you have to enter each link by hand. There is over 11 gigs, ~2,500 files on this server & I just don't have time to sit here & enter every one in by hand.
Does anyone know of a script that will do this automatically? just have to index local directories.
Thank you on Advance
_Lobo_
NogDog
11-23-2004, 10:14 AM
Some of the user suggestions on the www.php.net opendir() page (http://us2.php.net/manual/en/function.opendir.php) might be useful.
DJsAC
11-23-2004, 03:12 PM
Or you could use this:
Very basic and has to be placed in every folder... :)
<table width="300" align="center">
<tr align="center"><td align="center"><hr color='#000000'>
<?PHP
$curdir = "./"; // gives the directory its CURRENTLY in.
//echo $curdir."<br>"; //dir of the files you want to read.
$dir = $curdir;
$handle = opendir($dir);
$num = 0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.php" && $file != ".htaccess" && $file != ".htpasswd") {
list($filename,$extension) = explode(".",$file);
$filename = ucwords($filename);
$filesize = (filesize($dir.$file)/1000);
echo " <a href=".$dir.$file." target=\"_blank\">".$filename."</a><br>";
if (strlen($extension) <= 2) {$extension = "folder"; }
echo "Type: ".strtoupper($extension)." - ".$filesize." Kb<br><hr color='#000000'>";
$num++;
}
}
closedir($handle); ?>
</td></tr>
</table>
_LOBO_
11-24-2004, 01:59 AM
this is great!
ok it show me this is error when it find a folder:
Notice: Undefined offset: 1 in d:\program files\easyphp1-7\www\test.php on line 19
and just one small thing, it is posible to indexing one level more low (inside folders) ?
This script it will be great to keep updated a list of links on a website. :D
Thank you! again :p
DJsAC
11-24-2004, 07:02 AM
For 1 level subfolders as wel:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<table width="500" align="left">
<tr align="left"><td align="left"><hr color='#000000'>
<?PHP
$curdir = "./"; // gives the directory its CURRENTLY in.
$dir = $curdir;
$handle = opendir($dir);
$num = 0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.php" && $file != ".htaccess" && $file != ".htpasswd") {
//$rev = strrev($file);
//list($extension,$filename) = explode(".",$rev,2);
$data = explode(".",$file);
#print_r($data);
//echo $subdata;
$cnt = count($data);
#echo "<br>cnt: ".$cnt."<br>";
if($cnt==1)
{
$filename = $data[0];
$extension = "";
#echo "filename: ".$filename."<br>";
#echo "extension: ".$extension."<br>";
}
else {
$extension = $data[$cnt-1];
$filename = $data[0];
for($i=1;$i<$cnt-1;$i++)
{
$filename .= ".".$data[$i];
}
#echo "filename: ".$filename."<br>";
#echo "extension: ".$extension."<br>";
}
$filename = ucwords($filename);
$filesize = (filesize($dir.$file)/1000);
echo " <a href=".$dir.$file." target=\"_blank\">".$filename."</a><br>";
//echo "file: ".$file."<br>";
//echo "dir: ".$dir."<br>";
if (strlen($extension) < 3) {$extension = "folder"; $loop = true; }
else {$loop = false; }
if($loop != true)
{
echo "Type: ".strtoupper($extension)." - ".$filesize." Kb<br><hr color='#000000'>";
}
if($loop == true)
{
echo "Type: ".strtoupper($extension)."<br><hr color='#FF0000'>";
//---------------//
$cursubdir = "./".$file; // gives the directory its CURRENTLY in.
$subdir = $cursubdir;
#echo $subdir.":subdir<br>";
$subhandle = opendir($subdir);
$subnum = 0;
while (false !== ($subfile = readdir($subhandle))) {
if ($subfile != "." && $subfile != ".." && $subfile != "index.php" && $subfile != ".htaccess" && $subfile != ".htpasswd") {
/*$subrev = strrev($subfile);
list($subextension,$subfilename) = explode(".",$subrev,2);
$subfilename = strrev($subfilename);
echo "Filename: ".$subfilename."<br>";
$subfilename = ucwords($subfilename);
echo "filename ucase: ".$subfilename."<br>";
$subextension = strrev($subextension);
echo "Extension: ".$subextension."<br>";
echo "file: ".$subfile."<br>";
echo "dir: ".$subdir."<br>";
*/
$subdata = explode(".",$subfile);
#print_r($subdata);
//echo $subdata;
$subcnt = count($subdata);
#echo "<br>cnt: ".$subcnt."<br>";
if($subcnt==1)
{
$subfilename = $subdata[0];
$subextension = "";
#echo "Subfilename: ".$subfilename."<br>";
#echo "Subextension: ".$subextension."<br>";
}
else {
$subextension = $subdata[$subcnt-1];
$subfilename = $subdata[0];
for($subi=1;$subi<$subcnt-1;$subi++)
{
$subfilename .= ".".$subdata[$subi];
}
#echo "Subfilename: ".$subfilename."<br>";
#echo "Subextension: ".$subextension."<br>";
}
$subfilename = ucwords($subfilename);
$subfilesize = (filesize($subdir."/".$subfile)/1000);
echo " <a href=".$subdir."/".$subfile." target=\"_blank\">".$subfilename."</a><br>";
if (strlen($subextension) < 3) {$subextension = "folder"; $subloop = true; }
else {$subloop = false; }
if($subloop != true)
{
echo " Type: ".strtoupper($subextension)." - ".$subfilesize." Kb<br><hr color='#000000'>";
}
if($subloop == true)
{
echo " Type: ".strtoupper($subextension)."<br><hr color='#000000'>";
}
$subnum++;
}
}
closedir($subhandle);
//---------------//
}
$num++;
}
}
closedir($handle); ?>
</td></tr>
</table>
</body>
</html>
_LOBO_
11-25-2004, 02:39 AM
your script is really cool !!:D
I have one questions if you want go one level more down and show diplay only the HTML/HTM files where I should make the changes on the script?
Thank you for this great script.
DJsAC do you have a website?
I'd recommend a recursive function to search through any level of directories automatically. There are some premade scripts like this at www.phpbuilder.net. Just thought I would mention it, since it could save you a great deal of code. ;)
DJsAC
11-25-2004, 04:07 AM
I don't have my own site yet, though I do have a couple of sites online that I built and keep up to date for different company's.
I editted the script so that it's easier to follow.
Be carefull that when you add more subdirectories you don't cause a server timeout. ;)
As for a recursive function, that would probably be better, but I couldn't figure out how to write it in such a way that it could call itself, and retain the original strings so it could continue down the list :)
wel, here goes:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<table width="500" align="left">
<tr align="left"><td align="left"><hr color='#000000'>
<?PHP
$spacer = " ";
$divider = " - ";
$curdir = "./"; // gives the directory its CURRENTLY in.
$dir = $curdir;
$handle = opendir($dir);
$num = 0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.php" && $file != ".htaccess" && $file != ".htpasswd") {
$data = explode(".",$file);
$cnt = count($data);
if($cnt==1)
{
$filename = $data[0];
$extension = "";
}
else
{
$extension = $data[$cnt-1];
$filename = $data[0];
for($i=1;$i<$cnt-1;$i++)
{
$filename .= ".".$data[$i];
}
}
$filename = ucwords($filename);
$filesize = (filesize($dir.$file)/1000);
echo $spacer."<a href=".$dir.$file." target=\"_blank\">".$filename."</a><br>";
if (strlen($extension) < 3)
{
$extension = "folder"; $loop = true;
}
else
{
$loop = false;
}
if($loop != true)
{
echo $spacer."Type: ".strtoupper($extension).$divider.$filesize." Kb<br><hr color='#000000'>";
}
if($loop == true)
{
echo $spacer."Type: ".strtoupper($extension)."<br><hr color='#FF0000'>";
//---------------//
$cursubdir = "./".$file; // gives the directory of SUBFOLDER in.
//Use the $??file string from the script above this one...
// To create more sublevels copy this script from
//---------------// to //--------------//
// and rename the variables so that they don't conflict with each other.
// See below for example.
$subdir = $cursubdir;
$subhandle = opendir($subdir);
$subnum = 0;
while (false !== ($subfile = readdir($subhandle)))
{
if ($subfile != "." && $subfile != ".." && $subfile != "index.php" && $subfile != ".htaccess" && $subfile != ".htpasswd")
{
$subdata = explode(".",$subfile);
$subcnt = count($subdata);
if($subcnt==1)
{
$subfilename = $subdata[0];
$subextension = "";
}
else
{
$subextension = $subdata[$subcnt-1];
$subfilename = $subdata[0];
for($subi=1;$subi<$subcnt-1;$subi++)
{
$subfilename .= ".".$subdata[$subi];
}
}
$subfilename = ucwords($subfilename);
$subfilesize = (filesize($subdir."/".$subfile)/1000);
echo $spacer.$spacer.$spacer."<a href=".$subdir."/".$subfile." target=\"_blank\">".$subfilename."</a><br>";
if (strlen($subextension) < 3)
{
$subextension = "folder"; $subloop = true;
}
else
{
$subloop = false;
}
if($subloop != true)
{
echo $spacer.$spacer.$spacer."Type: ".strtoupper($subextension).$divider.$subfilesize." Kb<br><hr color='#000000'>";
}
if($subloop == true)
{
echo $spacer.$spacer."Type: ".strtoupper($subextension)."<br><hr color='#00FF00'>";
//ENTER SUBFOLDER CODE HERE
//---------------//
$cursub2dir = "./".$subdir."/".$subfile; // gives the directory of SUBFOLDER in.
//Use the $??file string from the script above this one...
// IN THIS CASE: $subfile
//Rename all other strings to new ones. (for simplicity: subdir becomes sub2dir :) )
//Add $spacers to make the list more user friendly.
$sub2dir = $cursub2dir;
$sub2handle = opendir($sub2dir);
$sub2num = 0;
while (false !== ($sub2file = readdir($sub2handle)))
{
if ($sub2file != "." && $sub2file != ".." && $sub2file != "index.php" && $sub2file != ".htaccess" && $sub2file != ".htpasswd")
{
$sub2data = explode(".",$sub2file);
$sub2cnt = count($sub2data);
if($sub2cnt==1)
{
$sub2filename = $sub2data[0];
$sub2extension = "";
}
else
{
$sub2extension = $sub2data[$sub2cnt-1];
$sub2filename = $sub2data[0];
for($sub2i=1;$sub2i<$sub2cnt-1;$sub2i++)
{
$sub2filename .= ".".$sub2data[$sub2i];
}
}
$sub2filename = ucwords($sub2filename);
$sub2filesize = (filesize($sub2dir."/".$sub2file)/1000);
echo $spacer.$spacer.$spacer.$spacer.$spacer."<a href=".$sub2dir."/".$sub2file." target=\"_blank\">".$sub2filename."</a><br>";
if (strlen($sub2extension) < 3)
{
$sub2extension = "folder"; $sub2loop = true;
}
else
{
$sub2loop = false;
}
if($sub2loop != true)
{
echo $spacer.$spacer.$spacer.$spacer.$spacer."Type: ".strtoupper($sub2extension).$divider.$sub2filesize." Kb<br><hr color='#000000'>";
}
if($sub2loop == true)
{
echo $spacer.$spacer.$spacer.$spacer.$spacer."Type: ".strtoupper($sub2extension)."<br><hr color='#0000FF'>";
//ENTER SUBFOLDER CODE HERE
//
//ENTER SUBFOLDER CODE HERE
}
$sub2num++;
}
}
closedir($sub2handle);
//---------------//
//ENTER SUBFOLDER CODE HERE
}
$subnum++;
}
}
closedir($subhandle);
//---------------//
}
$num++;
}
}
closedir($handle); ?>
</td></tr>
</table>
</body>
</html>
_LOBO_
11-25-2004, 07:52 AM
cool, and there is any way to index only html/htm files ? :)
DJsAC
11-25-2004, 09:55 AM
_XXX_ --> stringchange in subscript :)
replace:
echo $spacer."<a href=".$_XXX_dir.$_XXX_file." target=\"_blank\">".$_XXX_filename."</a><br>";With:
if (($_XXX_extension == "html") OR ($_XXX_extension == "htm") OR ($_XXX_extension == "HTML") OR ($_XXX_extension == "HTM" OR (strlen($_XXX_extension) < 3)) {
$_XXX_html = true;
echo $spacer."<a href=".$_XXX_dir.$_XXX_file." target=\"_blank\">".$_XXX_filename."</a><br>";
}
else
{
$_XXX_html=false;
}
And replace:
if($_XXX_loop != true)
With:
if(($_XXX_loop != true) AND ($_XXX_html == true)
_LOBO_
11-25-2004, 10:23 AM
Thank you the first thing that I will do tomorrow morning is test this ;) .
_LOBO_
11-26-2004, 01:44 AM
is perfect, but the script still show folders and can't look for html files one level more low :o.
I don’t want to sound ungrateful, with out your help It’s impossible to me make a script like this, thank you again . :D
DJsAC
11-26-2004, 02:42 AM
_XXX_ --> stringchange in subscript :)
To have it not show the FOLDERS:
Change:
if (($_XXX_extension == "html") OR ($_XXX_extension == "htm") OR ($_XXX_extension == "HTML") OR ($_XXX_extension == "HTM" OR (strlen($_XXX_extension) < 3)) {
$_XXX_html = true;
echo $spacer."<a href=".$_XXX_dir.$_XXX_file." target=\"_blank\">".$_XXX_filename."</a><br>";
}
else
{
$_XXX_html=false;
}
To:
if (($_XXX_extension == "html") OR ($_XXX_extension == "htm") OR ($_XXX_extension == "HTML") OR ($_XXX_extension == "HTM")) {
$_XXX_html = true;
echo $spacer."<a href=".$_XXX_dir.$_XXX_file." target=\"_blank\">".$_XXX_filename."</a><br>";
}
else
{
$_XXX_html=false;
}
AND:
Change:
if($_XXX_loop == true)
{
echo $spacer."Type: ".strtoupper($_XXX_extension)."<br><hr color='#FF0000'>";
//---------------//
TO:
if($_XXX_loop == true)
{
//echo $spacer."Type: ".strtoupper($_XXX_extension)."<br><hr color='#FF0000'>";
//---------------//
-----------------------------------------------------------------
To Add one more level of subfolders, just copy the code between the //-------// lines and repeat in the specified region.
Change the strings to something else ($sub2... becomes sub3... for example) And change the $cur_XXX_dir string in the new section. :-)
If you look carefully you'll see that the code is allready repeating 3 times.
Systematically: (Letter): represents 1 repetition of code
(A)Mainfolder
(B)Sub
(C)Sub2
(/C)Finished sub2, back to sub
(/B)Finished sub, back to main
(/A)Finished main. Code Complete.
_LOBO_
11-26-2004, 03:41 AM
I get this:
Parse error: parse error, unexpected '{' in d:\program files\easyphp1-7\www\fitness_academy\events\test4.php on line 42
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<table width="500" align="left">
<tr align="left"><td align="left"><hr color='#000000'>
<?PHP
$spacer = " ";
$divider = " - ";
$curdir = "./"; // gives the directory its CURRENTLY in.
$dir = $curdir;
$handle = opendir($dir);
$num = 0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.php" && $file != ".htaccess" && $file != ".htpasswd") {
$data = explode(".",$file);
$cnt = count($data);
if($cnt==1)
{
$filename = $data[0];
$extension = "";
}
else
{
$extension = $data[$cnt-1];
$filename = $data[0];
for($i=1;$i<$cnt-1;$i++)
{
$filename .= ".".$data[$i];
}
}
$filename = ucwords($filename);
$filesize = (filesize($dir.$file)/1000);
//==============================NEW START
if (($_XXX_extension == "html") OR ($_XXX_extension == "htm") OR ($_XXX_extension == "HTML") OR ($_XXX_extension == "HTM")) {
$_XXX_html = true;
echo $spacer."<a href=".$_XXX_dir.$_XXX_file." target=\"_blank\">".$_XXX_filename."</a><br>";
}
else
{
$_XXX_html=false;
}
//==============================NEW END
if (strlen($extension) < 3)
{
$extension = "folder"; $loop = true;
}
else
{
$loop = false;
}
//==============================NEW START
if(($_XXX_loop != true) AND ($_XXX_html == true)
//==============================NEW END
{
echo $spacer."Type: ".strtoupper($extension).$divider.$filesize." Kb<br><hr color='#000000'>";
}
//==============================NEW START
if($_XXX_loop == true)
{
//echo $spacer."Type: ".strtoupper($_XXX_extension)."<br><hr color='#FF0000'>";
//==============================NEW END
$cursubdir = "./".$file; // gives the directory of SUBFOLDER in.
//Use the $??file string from the script above this one...
// To create more sublevels copy this script from
//---------------// to //--------------//
// and rename the variables so that they don't conflict with each other.
// See below for example.
$subdir = $cursubdir;
$subhandle = opendir($subdir);
$subnum = 0;
while (false !== ($subfile = readdir($subhandle)))
{
if ($subfile != "." && $subfile != ".." && $subfile != "index.php" && $subfile != ".htaccess" && $subfile != ".htpasswd")
{
$subdata = explode(".",$subfile);
$subcnt = count($subdata);
if($subcnt==1)
{
$subfilename = $subdata[0];
$subextension = "";
}
else
{
$subextension = $subdata[$subcnt-1];
$subfilename = $subdata[0];
for($subi=1;$subi<$subcnt-1;$subi++)
{
$subfilename .= ".".$subdata[$subi];
}
}
$subfilename = ucwords($subfilename);
$subfilesize = (filesize($subdir."/".$subfile)/1000);
echo $spacer.$spacer.$spacer."<a href=".$subdir."/".$subfile." target=\"_blank\">".$subfilename."</a><br>";
if (strlen($subextension) < 3)
{
$subextension = "folder"; $subloop = true;
}
else
{
$subloop = false;
}
if($subloop != true)
{
echo $spacer.$spacer.$spacer."Type: ".strtoupper($subextension).$divider.$subfilesize." Kb<br><hr color='#000000'>";
}
if($subloop == true)
{
echo $spacer.$spacer."Type: ".strtoupper($subextension)."<br><hr color='#00FF00'>";
//ENTER SUBFOLDER CODE HERE
//---------------//
$cursub2dir = "./".$subdir."/".$subfile; // gives the directory of SUBFOLDER in.
//Use the $??file string from the script above this one...
// IN THIS CASE: $subfile
//Rename all other strings to new ones. (for simplicity: subdir becomes sub2dir :) )
//Add $spacers to make the list more user friendly.
$sub2dir = $cursub2dir;
$sub2handle = opendir($sub2dir);
$sub2num = 0;
while (false !== ($sub2file = readdir($sub2handle)))
{
if ($sub2file != "." && $sub2file != ".." && $sub2file != "index.php" && $sub2file != ".htaccess" && $sub2file != ".htpasswd")
{
$sub2data = explode(".",$sub2file);
$sub2cnt = count($sub2data);
if($sub2cnt==1)
{
$sub2filename = $sub2data[0];
$sub2extension = "";
}
else
{
$sub2extension = $sub2data[$sub2cnt-1];
$sub2filename = $sub2data[0];
for($sub2i=1;$sub2i<$sub2cnt-1;$sub2i++)
{
$sub2filename .= ".".$sub2data[$sub2i];
}
}
$sub2filename = ucwords($sub2filename);
$sub2filesize = (filesize($sub2dir."/".$sub2file)/1000);
echo $spacer.$spacer.$spacer.$spacer.$spacer."<a href=".$sub2dir."/".$sub2file." target=\"_blank\">".$sub2filename."</a><br>";
if (strlen($sub2extension) < 3)
{
$sub2extension = "folder"; $sub2loop = true;
}
else
{
$sub2loop = false;
}
if($sub2loop != true)
{
echo $spacer.$spacer.$spacer.$spacer.$spacer."Type: ".strtoupper($sub2extension).$divider.$sub2filesize." Kb<br><hr color='#000000'>";
}
if($sub2loop == true)
{
echo $spacer.$spacer.$spacer.$spacer.$spacer."Type: ".strtoupper($sub2extension)."<br><hr color='#0000FF'>";
//ENTER SUBFOLDER CODE HERE
//
//ENTER SUBFOLDER CODE HERE
}
$sub2num++;
}
}
closedir($sub2handle);
//---------------//
//ENTER SUBFOLDER CODE HERE
}
$subnum++;
}
}
closedir($subhandle);
//---------------//
}
$num++;
}
}
closedir($handle); ?>
</td></tr>
</table>
</body>
</html>
DJsAC
11-26-2004, 03:48 AM
You'll have to change the _XXX_ so that the correspond to the script ;)
currenty the variables _XXX_etc don't exist.
If you fix that the error on line 42 will probably dissappear as wel.
_LOBO_
11-26-2004, 04:19 AM
I made all the changes that you show me before, but Im still get this error, why is that? wich other string I shoul be change?
if (($_XXX_extension == "html") OR ($_XXX_extension == "htm") OR ($_XXX_extension == "HTML") OR ($_XXX_extension == "HTM" OR (strlen(
DJsAC
11-26-2004, 04:59 AM
Here you go...
No Folders, HTML and HTM files in local Dir and 4 dir's deep.
_LOBO_
11-26-2004, 05:51 AM
A big Thank YOU !
_LOBO_
11-26-2004, 08:37 AM
(wrote on your abatar)PHP n00b :D YHEA RIGHT!!! :D
DJsAC
11-26-2004, 08:42 AM
I'm still learning :P
I've only been doing this for about a year now.
(Registered here when I started learning. (Sept 2003)) :cool: