Virtual folders for items in database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Virtual folders for items in database

Post by josh »

Here is my issue:
I have a script where users sign up for a free account and login and then can store text files and images to their account.. I want to now allow them to create sub directories (folders to organize files) on their account, but since I do not acutally create the files they are just in a database this might be slightly difficult

My solution so far
Add a column named `virtualpath` defaults to "/" (users 'root' folder) then when users create documents they can specify a folder name for instance if they put 'path' for the folder name the `virtualpath` will read `/path/` ...
Then on their file manager page it will show all their folders which they can browse.. i want them to be able to have unlimited subdirectories which means /path/path/1/1/1/1/1/1//1/1/1/1/1//1/1/etc/etc/etc........

I can list their folders with this code

Code: Select all

<? $user = $_SESSION['user'];
$user = secure($user);
$result=mysql_query("SELECT `virtualpath` FROM `texts` WHERE `user` = '$user' group by `virtualpath` ;") or die(mysql_error());
while ( list($path) = mysql_fetch_array($result)) {
	echo "<TR><TD>$path</td></tr>";
} ?>
but the problems I am faceing is only display the first directory in the path /path/to/file would display path/ on the home page then clicking path/ would allow them to see all folders and files within path/ so clicking path/to/ would show the folder path/to/file and all files in there...

The only thing i can think of right now is arrays?

$folders["/"]["path/"]["to/"]["file/"] or something like that... any ideas on simpiler ways to implement this? Or how I would dynamically name the arrays indexes like explodeing the paths i get from the database or something... ugh this is so confuseing
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You could add a parent field with the ID of the parent directory.

Or you could use LIKE in your queries to only get sub directories, for example:

SELECT `virtualpath` FROM `texts` WHERE `user` = '$user' AND `virtualpath` LIKE '/current/dir/%' group by `virtualpath`
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

hmm... ok how would I truncate a string after the Nth slash? For instance If the user is trying to access /path/to and I show the user has a file in /path/to/file/here I can count the /'s in the path they are in which is 2 then truncate the 2nd slash and everything before it, then truncate everything after but not includeing the first slash (first slash meaning the first slash that is in the path after i truncated the begining) that way I could list all the folders inside the folder they are in.

Sorry if this makes no sense... basically I need to write a function so I can do this

Code: Select all

<?php
// the path a file is in
$path="/path/to/folder/another/folder"
echo getvirtualpaths("/path/to/", $path); // needs to return "/path/to/folder/" or "/folder/" or "folder/" or something
?>
How would I go about writeing getvirtualpaths(); ?? Any ideas?
Post Reply