Computing.Net > Forums > Web Development > autoscan folder and create links

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

autoscan folder and create links

Reply to Message Icon

Original Message
Name: dave c
Date: January 3, 2005 at 06:42:23 Pacific
Subject: autoscan folder and create links
OS: varies
CPU/Ram: varies
Comment:

Hi, I'm not expert, or anything close, but I am working on my company's intranet. What I would like is to find a program that will scan a folder, then create a webpage that has a link to all the files in that directory.

For example, I have a script that pulls data out of our mainframe and exports them into a pdf file in a folder on my 2k server. Instead of maunally updating the web page every day by adding a new link by hand, I want the web page to be automatically updated by scanning the folder, and removing links to files we've deleted, and adding links to files we've added.
Any suggestions?

We're a non-profit org, so cheaper is better!

Thanks in advance for any help!


Report Offensive Message For Removal


Response Number 1
Name: SN
Date: January 3, 2005 at 07:43:47 Pacific
Reply: (edit)

Here's a simple PHP script that does what you want...As well as has a place to upload a new file. It should be placed in the dir that you want to index and named index.php. Of course, it can be modified to be named something different or in a different dir...Just let me know.

-SN

<?php

if ($_POST['submit'])
{

copy($_FILES['file']['tmp_name'], $_FILES['file']['name']);
}

?>
<html>
<head><title>page</title>
</head>
<body>
<?php
$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false)
{
if ($file != "." and $file != ".." and $file != "index.php")
{
array_push($files, $file);
}
}
closedir($dir);
sort($files);
foreach ($files as $file)
print "[A href='$file'>$file</a]
";
?>
<form action='<?php echo $PHP_SELF ?>' enctype='multipart/form-data' method='POST'>
Add a new file: <input type='file' name='file'>

<input type='submit' name='submit' value='Add File!'>
</form>
</body>
</html>


Report Offensive Follow Up For Removal

Response Number 2
Name: dave c
Date: January 3, 2005 at 11:33:08 Pacific
Reply: (edit)

Thank you so much for your effort!

Again, I'm no where near expert at this, wht you just wrote is almost gibberish to me!

I copied and pasted it in notepad and saved it as index.php. I placed it in a directory with a boatload of pdf files. I opened it with IE, and it opened a web page, but it only has the form part on it.

I noticed that even though it's right in the wwwroot folder (That's where they placed the pdf files), when I browse to it on another computer, I get 'page cannot be displayed'. Do I need to do anything to enable php?


Report Offensive Follow Up For Removal

Response Number 3
Name: SN
Date: January 3, 2005 at 12:24:17 Pacific
Reply: (edit)

Hi Dave-
It sounds like you're using IIS, and will need to install PHP on the webserver. Download the installer on the PHP.net download page. You'll need the PHP 4.3.10 installer.

Do you know HTML, or any programming languages? If you know HTML and can figure out what an array is, the code below should be fairly straightforward. I've documented it a bit and put links to PHP's documentation as well.

<?php
//if they clicked the 'submit' button,
//upload the file they chose.
//handling file uploads
if ($_POST['submit'])
{
 copy($_FILES['file']['tmp_name'], $_FILES['file']['name']);
}

?>
<html>
<head><title>page</title>
</head>
<body>
<?php
//open the current directory (".")
$dir=opendir(".");

//make an array we'll use to store the files we find
$files=array();

//read each file or folder in the dir and
//put it into the $files array if it's not
//this file, ".", or ".."
//reading files & folders in a directory
while (($file=readdir($dir)) !== false)
{
   if ($file != "." and $file != ".." and $file != "index.php")
   {
    array_push($files, $file);
   }
}
//close the directory
closedir($dir);

//sort the files so they print out in
//alphabetical order
sort($files);

//go through each file we found and print
//out a link to it. change [ and ] to < and >
foreach ($files as $file)
  print "[A href='$file'>$file</a]";
?>
<form action='<?php echo $PHP_SELF ?>' enctype='multipart/form-data' method='POST'>
Add a new file: <input type='file' name='file'>

<input type='submit' name='submit' value='Add File!'>
</form>
</body>
</html>


Report Offensive Follow Up For Removal

Response Number 4
Name: dave c
Date: January 3, 2005 at 13:34:44 Pacific
Reply: (edit)

Thank you.. I know some HTML, but nothing advanced.

I am going to take some time to look through what you wrote, and read up on the parts I don't understand. Then I will give it a shot, and hopefully between wht I've learned by reading, and what you've provided me, it will help. If not I will post again.

Thank you so much for your help!


Report Offensive Follow Up For Removal

Response Number 5
Name: dave c
Date: January 3, 2005 at 14:32:11 Pacific
Reply: (edit)

ok, it didn't work as written, but it was close enough for me to play with until I got it to do what I wanted it to do.

I installed PHP on a dummy server with IIS, and modified the script to read as follows:

<html>
<head><title>page</title>
</head>
<body>
<?php
$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false)
{
if ($file != "." and $file != ".." and $file != "index.php")
{
array_push($files, $file);
}
}
closedir($dir);
sort($files);
foreach ($files as $file)
print "<A .href='$file'>$file</.a>
<B.R>";
?>

</body>
</html>

(ignore periods in lines 18 + 19, they were put there to display correectly on this page)

This works great, gives me a blank page with links to everything in that folder.

Now all I have to do is implement it into a normal web page like the other pages on my intranet and I'm set. I'm thrilled, thank you!

However, there are a couple of minor issues maybe you can help me with?

1) One of the things we are using this is for meeting minutes. We have many board meetings, committee meetings, ect, and we didn't want to have to hard code a web page after each one (who would?). With this script, we can put all th eminteus (in pdf format) in a folder, and put a link to /minutes/index.php and it will list all the meeting minutes as links, and at the end is a link to index.php (since that's a file in the directory). Is there any way we can eliminate that? (Such as an if/then statement?)

2) Also, how about folders instead of just files?
I have a working test run on my dummy PC with some MIDI files in it. From anothe rmachine, I run /computer/midi/index.php, and get an accurate list. I add midis, refresh the web page, and it refreshed the list. I decided to get specific, and added a folder called "classical". It instantly became available as a link. I put some classical midi's in that folder, put a copy of index.php in that folder, and on remote machine typed in "http://computer/midi/classical/index.php" and again it was perfect.

But when I go to /midi/index.php, it shows a link to "classical", I click on it, and get "You are not authorized", like an FPT error or something.

Is there anyway for it to recognize folders, and when clicked, go to that folder's individual index.php?


Thank you so much in advance. If you can't get it, no worries, I'll study whatever I have to to learn it, you have provided me with the foundation I need to do so (I didn't even know where to look before!)

I don't mind researching it, in fact I think I rather enjoy this stuff and might learn it anyway, but if it's something that is second nature to you, then it doesn't hurt to ask!!


Report Offensive Follow Up For Removal


Response Number 6
Name: SN
Date: January 3, 2005 at 15:18:15 Pacific
Reply: (edit)

The stuff you mentioned is pretty easy. Here's some ideas to get you started:
1. It sounds like you're going to have index.php in quite a few folders. It would be better to have one copy of the file that can handle several folders. So what we'll do is modify the script to take in a directory from the URL rather than assume we want the current dir.

2. I thought the script should not print out a link to index.php, but apparently I was wrong. We'll modify it to not show links for any .php files.

3. Adding support for folders isn't hard...PHP has a function is_dir that will tell us that the current item being read is a directory and handle that case separate.

In case you're curious, the "You are not authorized" error is because the script as it is prints out a link to "/midi/classical/" without the index.php, so you get an IIS error indicating that you can't browse the contents of that particular directory.

I should also mention that this is actually possible without any php at all...In IIS you can just check the "Directory Browsing" checkbox under "Home Directory" on the default website's properties and it will do all this stuff for you.

Finally, the modified script:

<html>
<head><title>page</title>
</head>
<body>
<?php
//first get the dir value from the URL.
//ie index.php?dir=/midi/classical
//if it's not found, just choose the current dir

if ($_GET['dir'])
$source_dir = $_GET['dir'];
else
$source_dir = ".";

$dir=opendir($source_dir);
$files=array();
while (($file=readdir($dir)) !== false)
{
//changed to not show any .php files.
//strpos function
//strtolower ensures that case is ignored
if ($file != "." && $file != ".." && strpos(strtolower($file),".php") === false)
{
array_push($files, $file);
}
}
closedir($dir);
sort($files);
foreach ($files as $file)
{
//if it's a dir, print out a link to this
//script with the new dir in the URL.
if (is_dir($source_dir."/".$file))
print "<A .href='".$_SERVER['PHP_SELF']."?dir=$source_dir/$file'>$file<.br>";
else
print "<A .href='$file'>$file<.BR>";
}
?>

</body>
</html>


Report Offensive Follow Up For Removal

Response Number 7
Name: dave c
Date: January 6, 2005 at 06:49:43 Pacific
Reply: (edit)

Sweet. Had a little problem with it, kept coming up with an invalid directory error, yet it listed the files accurately.

I changed one line to read if (@$_GET['dir']) (I added teh @ sign) and now it works perfectly. If I add that to any of our web pages, it will list all the files in that directory, and can browse to subdirectories, all I have to do is add a web page into the new directory, easy enough to do.

After that, just share the folders, and create shortcuts on user's desktops to drop new files in there, and they will automatically be added to the web page.

Thank you so much.. we thought we would have to pay for a program to do this.

I owe you!

-Dave


Report Offensive Follow Up For Removal






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software