473,320 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to add an index file to all subdirectories

170 100+
Hello all,

I've done something a bit stupid and am hoping some kind soul out there can help me out.

There's a piece of code that goes through and creates a high number of subdirectories within subdirectories, which is fine. Initially I forgot to create an index.html file in each directory to prevent viewing of its folders and files without people being logged in. I've fixed it now, but there are a whole heap of directories without this file.

Is there some piece of code which, if I specify the top directory, will go through and add the index file?

I have the code the add the file. I'm stuck on getting the code to go through all subdirectories within subdirectories etc. Here's what I have so far:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function addindex($dir)
  3. {
  4. if ($handle = opendir($dir))
  5. {
  6.     while (false !== ($file = readdir($handle))) {
  7.         if ($file != "." && $file != "..") {
  8.  
  9.             if(is_dir($dir.$file))
  10.             {
  11.                     $indexfile = $dir.$file."/index.html";
  12.                     $filehandle = fopen($indexfile, 'w') or die("can't open file");
  13.                     fclose($filehandle);
  14.             }
  15.         }
  16.     }
  17.     closedir($handle);
  18. }
  19. }
  20. addindex('mydir/');
  21. ?>
Dec 2 '08 #1
10 2597
dlite922
1,584 Expert 1GB
This is now how you protect your directories. An index.html file just gives them something to see. If I really wanted to see what's in the directory, I'd have no problem accessing it.

use a .htaccess file if your running Apache, or don't put the directories under the site directory, put them elsewhere.




Dan
Dec 3 '08 #2
beary
170 100+
Hey Dan,

Thanks for the reply.

@dlite922
The point is that if someone works out the user's directory, and they enter that path into the address bar of the browser, all they'll see is a blank file, not the listing of folders and files. Surely having the index.html file in there will accomplish this?

Can you please expand on how you could access the files in a directory.

use a .htaccess file if your running Apache, or don't put the directories under the site directory, put them elsewhere. Dan
I don't think this solution will work as it's possible for there to be subdirectories within subdirectories within subdirectories.

Anyway, I'm still interested in how to do something to every subfolder in a particular directory, whether it's adding an index file or doing something else.
Dec 3 '08 #3
dlite922
1,584 Expert 1GB
@beary
There are some crawler type software that do it. I have not used it, but one method would be direct access to the file if I guess or know the file name I'm looking for.

I'm just saying if there is sensitive data in there that you need protected, this is not a way to do it. If it doesn't really matter if a hacker has the files, then this could work for the average user. I still don't understand why you would want folders that are not public be on a public site.

You can control access via a .htaccess:

RewriteRule ^includes /this.file.does.not.exist
RewriteRule ^errors /this.file.does.not.exist



The URL i gave you does show you have a recursion can be used to traverse a directory tree.





Dan
Dec 3 '08 #4
beary
170 100+
@dlite922
Ok, let me try to give more detail:

It's a site run at my workplace (which happens to be a school). It's hosted on a web share on the school's domain name, which means the main school website is publicly accessible. My site lives on a web share which is a subdirectory of the main site.

To access this site, users (in this case, teachers or students) must enter their username or password. Without that, it won't log them in. The structure goes

teachuser/class/students/studusername/

eg. jenkins/9_maths/students/citizen_john

The student John Citizen can access anything in his folder, and theoretically other students can't. But John Citizen works out that since his folder path is jenkins/9_maths/students/citizen_john, then his classmate's path must be jenkins/9_maths/students/jones_mary and he enters that in the browser and it shows all her files. Not so good.

But with an index file, it'll only show a blank page to John, not her files. This seems to achieve that goal, however...

If I don't log in at all, and then type in jenkins/9_maths/students/citizen_john I get the standard directory layout. I realise this is a potentially big problem, for security and privacy issues.

So I guess my aim is this: there are multiple teachers using the site. For each teacher, there are multiple classes. For each class, there are teacher-uploaded files and also a folder called students. In that folder are the folders for each student in the class. In each of those folders are student files

Eg. Teacher Jenkins teaches a Maths class with 3 students. His folders are

jenkins/
jenkins/maths/ [in this folder there are normal files and possibly other folders]
jenkins/maths/students/
jenkins/maths/students/blunt_billy/
jenkins/maths/students/jones_mary/
jenkins/maths/students/citizen_john/

and in each of the above 3 folders there will be files.

Basically every folder and file under jenkins/ needs to be somehow protected. I fear this won't make too much sense, but assuming it does, what do you suggest?
Dec 3 '08 #5
dlite922
1,584 Expert 1GB
Wow, big project. In your case, the index file does you no good.

if i'm citizen john, how does it know i'm citizen john? are you going to code every index file to check a session variable against a static string or the parent folder name?

You really should be doing this in a database. This needs to be a secure application since there could be hundreds of students, it's not feasible to hand manage each of their folders.

You need a database that has a list of all teachers, students and classes. Then a table that matches the three:

teacherID, studentID, classID

where each is the corresponding foreign key to the three tables. Going back to the reason you're doing folders, you want the students and teachers to save files to these locations?

if so you can have a file table. Each file is attached to a student or teacher ID. the file itself could be uploaded and saved in a folder that has the user (student or teacher) ID as the name, or perhaps the firstName and lastName of the owner. This directory does not have to be "deep", only one or two subdirectory since it is assumed no student can have the same ID (no teacher with the same ID), so there won't be a conflict.

How it would work:

Once a student logs in, it will see the classes that he is enrolled in. he clicks on a class and sees the list of files he has. He can click on a file to download it or use the upload file feature to add a file. (you can also record time uploaded for teacher's use)

If a teacher logs in, it will see the classes he or she is teaching. Clicking on a class gives the list of students in the class (perhaps you can allow the teacher to store a grade or grade per assignment functionality as well). If a student name is clicked, the teacher will see the same files the student does.

This is a portal and you could build your own simple one, or you could just buy one that does much more than manage files. I currently use a portal for college that lets me interact on a forum, see my assignments, my grades for each of those. content and files teacher has upload as well as files that I have uploaded for grading.

myCampus






Dan
Dec 3 '08 #6
beary
170 100+
@dlite922
He he! It already does all of what you've mentioned below and far more. It's a completely database (mysql) driven site. The db has 38 tables, and it's all normalised. Everything has been meticulously thought out and constructed so there's no duplication in the tables and the code I have written works incredibly well. It's quick; it does exactly what I need it to do. But you're right, it is a big project, which is why so far I've probably spent over 500 hours of my own time on it.

if i'm citizen john, how does it know i'm citizen john? are you going to code every index file to check a session variable against a static string or the parent folder name?
Of course not. When the user logs in, his username, password etc is stored as a session variable. Those variables are available to every page. It knows you're John Citizen because you logged in as John Citizen and your unique username and password as stored as session variables. All this happens in 1 file which is included on any other file I want. Believe me, it works...

You really should be doing this in a database..SNIP...
See 1st paragraph.

Going back to the reason you're doing folders, you want the students and teachers to save files to these locations?
Yes. They can already do that as required. Students can save files to their own folder (and only their own folder) and any teacher of that student can see the files of any students in their own class, but no students who aren't in their class.


How it would work:

Once a student logs in, it will see the classes that he is enrolled in. he clicks on a class and sees the list of files he has. He can click on a file to download it or use the upload file feature to add a file. (you can also record time uploaded for teacher's use)

If a teacher logs in, it will see the classes he or she is teaching. Clicking on a class gives the list of students in the class (perhaps you can allow the teacher to store a grade or grade per assignment functionality as well). If a student name is clicked, the teacher will see the same files the student does.
Believe me, it does exactly this plus about another hundred things. It's been completely thought through.

This is a portal and you could build your own simple one, or you could just buy one that does much more than manage files.
Dan
As I've said, I have built one, and it's far from simple.

The last thing I want is for all these responses to come across as if I'm being arrogant about it all. But there's been an awful lot of time, thought and effort put into this. I'm not a newbie :) The site is completely functional. Ie. it does exactly what I wanted it to do when I set out to make it.

But the fact that it's functional doesn't guarantee its security. And that's where I need the advice of others re security and privacy. I like structure and organisation and I think the directory structure that I described is clear and logical. You've alerted me to the fact that having an index file won't help, which I appreciate. now I need to know ways of making those directories secure. Again, thanks for your conversation so far. I hope it continues.
Dec 3 '08 #7
Markus
6,050 Expert 4TB
So you want to go through every folder under a certain directory and insert an index file? OK, recursion is what you're looking for. Google will give you many recursive tutorials - then you just need to (in the function) check for an index file, if it's not there, create one.

Here's a good tutorial - scroll down for the folder recursion.
Dec 3 '08 #8
beary
170 100+
@Markus
Markus,

Thanks heaps for this solution to my original question.

Cheers
Dec 3 '08 #9
Markus
6,050 Expert 4TB
@beary
No problamo.

See you around,
Markus.
Dec 3 '08 #10
dlite922
1,584 Expert 1GB
@beary
Wow ... could have saved my breath then. I apologize. We get a lot of newbie questions here and sometimes we just work in that mode and don't realize when a real programmer is asking. I do tend to be long winded. so...

I'll cut this short.

Good luck with your app and future advances!



Dan
Dec 5 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Lab309 | last post by:
The problem I'm trying to solve is as follows: The website has two subdirectories: /ordinary and /phpstuff. Users typing hostname/ordinary get the file index.htm by default, and this works fine. ...
5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
3
by: Greg Yasko | last post by:
Hi. Does anyone know if there's an equivalent of Perl's file::find module in Python? It traverses a directory. I've googled extensively and checked this newsgroup and can't find anything like it...
5
by: Thomas Philips | last post by:
I want to get a list of all subdirectories of a given directory, and within each directory, change the name of each file using a predetermined rule. The function glob in module glob will give me...
4
by: Ed | last post by:
Hello, I took a course in asp about 2 years ago and I was practicing with IIS 5.0. Then I put it down for a while. Now trying to get back to it. I can't run asp files from subdirectories of...
1
by: android | last post by:
<Directory xmlns="http://tempuri.org/test.xsd" name="root"> <file name="File1"></file> <file name="File2"></file> <Directory name="Dir1"> <file name="File1"></file> <file name="File2"></file>...
11
by: G Patel | last post by:
Can anways explain why many multi-file programs I've seen don't have a main.c file, instead main() is hidden in a file with a different name? Is this just a preference, or is there an unspoken rule...
64
by: Manfred Kooistra | last post by:
I am building a website with identical content in four different languages. On a first visit, the search engine determines the language of the content by the IP address of the visitor. What the...
1
by: w33d5 | last post by:
I'm looking for a little help in making an index file to read the subdirectories in the same folder and display the links in a table. i am loading photo galleries using google's picasa onto my...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.