473,789 Members | 3,013 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to add an index file to all subdirectories

170 New Member
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 2635
dlite922
1,584 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
@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 New Member
@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 Recognized Expert Top Contributor
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 New Member
@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 Recognized Expert Expert
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 New Member
@Markus
Markus,

Thanks heaps for this solution to my original question.

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

See you around,
Markus.
Dec 3 '08 #10

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

Similar topics

7
6329
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. Users typing hostname/phpstuff don't get anything because there is no index.htm there, only index.php. What can I put in an index.htm file that'll invoke the index.php file? Or is there a setting in the Apache 2.0 server I've overlooked? ...
5
3349
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 charcters in it, that the code needs to run until all bad characters are gone? For example, if a file has the name "<bad*mac\file" the program has to run 3 times to get all three bad chars out of the file name. The passes look like this:
3
7005
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 for Python. I'd be using it in Linux to regularly search for files with a script. Is this a good application for Python or does Perl have more functionality for quick & simple scripting in Linux? Thanks.
5
3338
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 a list of all the subdirectories, and by looping over this list and repeatedly calling glob, I can create lists of files within each directory, which are easily edited to create lists of new filenames. How then can I change the existing...
4
3217
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 my root directory, but I can run asp files from the root directory of my website and I can run htm files from the subdirectories. If I run localhost/mytest.asp
1
2099
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> <Directory name="Dir1"> <file name="File1"></file> <file name="File2"></file> </Directory>
11
1643
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 (or advantage). I've been calling the file with main, main.c, and I was wondering if there are any caveats against this (that might prevent me from being laughed at or something).
64
6423
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 user sees is content in only one language at a time. He or she can then switch to another language and set this as the preferred language, but again he or she sees content in only this one other language. The question now is: How do I get search...
1
1312
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 host and since picasa exports the albums as folders (I am using a simpleviewer template) i would like to use a script to read the subdirectories in the \photos directory (where the index file would also be located) and display the folders in a clean...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.