play_ Posted December 13, 2007 Share Posted December 13, 2007 ??? ??? I've been working on this image uploading site. I can't figure out how to code it though. (mainly the subfolders part) Concept: User makes account. For example, SoccerFan12. When the account is created, i have it so it creates a folder under a folder called 'user' (all main folders are under the 'user' folder) So, SoccerFan12's main album is SoccerFan12. So SoccerFan12 uploads a picture. It gets stored under his main folder, (which is his username ). SoccerFan12 leaves the site, comesback tomorrow, logins, and the url would look like: www.site.com/user.php?soccerfan12 So now he sees all the pictures on the SoccerFan12 folder. Ok. so far it's easy. Now, soccerfan12 wants to create a subfolder (album inside his main album). Let's say, 'soccer_matches'. so, he goes to 'create an album here' and it creates a folder under SoccerFan12. So now the hierarchy soccerfan12/soccer_matches/ So, the url would look something like this: www.site.com/user.php?soccerfan12&album=soccer_matches Now it gets complicated. Now soccerfan12 wants to create a folder inside soccer_matches. Let's call it 'state_finals' 1. I would to know which album he is 'in' at the moment. I guess this could be accomplished by using chdir(). grab the value of 'album' in the url and use chdir to switch to it. So when he creates the folder called 'states_finals' under 'soccer_matches'. How could i link to this? www.site.com/user.php?soccerfan12&album=soccer_matches& ??? And how should the database be structured? should i store the albums created in the database? or just the image and the path to the album? something like userID | image_path | album_name | image_name | -----------------+----------------------------------------------+-----------------+-------------+ 1 | /soccerfan12/socce_matches/states_finals/ | states_finals | score.jpg | I know this is kind of a lot to go over. I'm by no means asking for code to be written. Just a logic on how it should be gone, and the best way to set up the database. thanks Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 its the classic dig theory. What needs to happen is not make subfolders, but instead make pseudo subfolders in mysql. You have a table called folders looks like FolderID FolderName ParentID UserID First off you don't need a folder for each user, rename on upload like USERNAME_FOLDERID_IMAGEID.EXTENSION that will save you a ton of issues. So when they query for images in a folder you find images matching the USERNAME_FOLDERID now to make folders you say. User adds a "folder" all you do is add to this folder table a row, the parent id will be the root folder of hte user (Auto Inclemently assigned, lets say socceruser root is folder 5) So they have make a subfolder called Saturday's Game in the Root from the auto incremented ID it becomes folder number 25 now they make a folder in Saturday's Game called First Half its row is like this FolderID: Auto Incremented Name: First Half ParentID: 5 UserID: SOccerUsers ID Num Make sense? Your queries get a bit complicated, you will want to use a GROUP BY to get them by ParentID and then you can easily query them in a batch sorta, that or use ajax to expand folders. Edit: You will need to slightly mod that data table on the individual images to reflect their folderID number they are "stored" in. Your concept is almost there, but let your database organize stuff over your physical design. If you still want to have .htaccess mods you can make it so when a person tries to go to http://yoursite.com/images/soccerfan12/saturdaysgame/firsthalf /1.jpg to really load a php file that is like displayimage.php?user=soccerfan&folder=saturdaysgame&folder=firsthalf&imge=1.jpg the only issue I see is some how you are going to have to relate that face that its the "last folder" which arrises the issue of duplicate subfolder names in different parent folders, you might need to carry across a full contex path in that Folders table to prevent a duplicate mis query. Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted December 13, 2007 Share Posted December 13, 2007 its the classic dig theory. What needs to happen is not make subfolders, but instead make pseudo subfolders in mysql. You have a table called folders looks like FolderID FolderName ParentID UserID First off you don't need a folder for each user, rename on upload like USERNAME_FOLDERID_IMAGEID.EXTENSION that will save you a ton of issues. So when they query for images in a folder you find images matching the USERNAME_FOLDERID now to make folders you say. User adds a "folder" all you do is add to this folder table a row, the parent id will be the root folder of hte user (Auto Inclemently assigned, lets say socceruser root is folder 5) So they have make a subfolder called Saturday's Game in the Root from the auto incremented ID it becomes folder number 25 now they make a folder in Saturday's Game called First Half its row is like this FolderID: Auto Incremented Name: First Half ParentID: 5 UserID: SOccerUsers ID Num Make sense? Your queries get a bit complicated, you will want to use a GROUP BY to get them by ParentID and then you can easily query them in a batch sorta, that or use ajax to expand folders. Edit: You will need to slightly mod that data table on the individual images to reflect their folderID number they are "stored" in. Your concept is almost there, but let your database organize stuff over your physical design. If you still want to have .htaccess mods you can make it so when a person tries to go to http://yoursite.com/images/soccerfan12/saturdaysgame/firsthalf /1.jpg to really load a php file that is like displayimage.php?user=soccerfan&folder=saturdaysgame&folder=firsthalf&imge=1.jpg the only issue I see is some how you are going to have to relate that face that its the "last folder" which arrises the issue of duplicate subfolder names in different parent folders, you might need to carry across a full contex path in that Folders table to prevent a duplicate mis query. That's a very simple and elegant way to handle a problem I've been trying to find a solution for... You should do a write up or tutorial on this if you haven't already. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 I believe a wiki exist on the theory I presented, its called dig theory or the bunny grid or something like that. Quote Link to comment Share on other sites More sharing options...
corylulu Posted December 13, 2007 Share Posted December 13, 2007 MySQL is gonna be your help in this. creating series of subfolders is gonna make the code extremely complicated and have to be altered by which folder you are already in. Quote Link to comment Share on other sites More sharing options...
play_ Posted December 14, 2007 Author Share Posted December 14, 2007 Very nice post cooldude, thanks. One thing. "First off you don't need a folder for each user, rename on upload like USERNAME_FOLDERID_IMAGEID.EXTENSION" So user uploages soccergame.jpg, and what you suggested is, rename it to soccerdude_5_soccergame.jpg. right? but what if i wanna allow the user to rename his pictures after it's been uploaded? also, if i put all images on one folder, what if they have the same name? I know i could check and rename, but for example, on photobucket, i can have the same picture name in two different albums. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 14, 2007 Share Posted December 14, 2007 you need to treat the physical file as nothing more but as an extension of mysql. So you need to reference it from mysql, the image name comments date etc are all stored in your database (or meta data in it if its there). So when soccer person uploads an image the script is like this <?php if(IS A VALID IMAGE){ //Open SQL Connection $q = "Insert into` Images (OwnerID, FolderID, Date, Name, etc.) Values($_SESSION['OwnerID'], $_POST['UploadID'], $date, $name, $etc.)"; $r = mysql_query($q) or die(mysql_error()); $upload_id = mysql_insert_id(); //Rename Upload file to $upload_id.jpg } ?> So when they try and view an image your htaccess actually executes a script that runs a sql query for an image with that owner id, that folder id and that name. Make a bit more sense? Quote Link to comment Share on other sites More sharing options...
play_ Posted December 15, 2007 Author Share Posted December 15, 2007 oh i think so. So i do this: "First off you don't need a folder for each user, rename on upload like USERNAME_FOLDERID_IMAGEID.EXTENSION". So physical folder, it will be something like, soccerdude_5_56.jpg, (this way names wont' conflict). but in sql, it will store the name of image it was when he uploaded it. like nightgame.jpg so then when the user links a friend to something like www.site.com/user/soccerdude/julygames/firstgame/nightgame.jpg the script would look in sql for 'nightgame' jpg , whose parent folder is firstgame's ID. if thats what you meant, then yea i think i got it. Quote Link to comment Share on other sites More sharing options...
play_ Posted December 15, 2007 Author Share Posted December 15, 2007 also, tried googling for bunny grid theory php, grid theory php....nothing relevant came up. =| Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 15, 2007 Share Posted December 15, 2007 yeah you got it, I've heard digg and bunny theroy before, the bunny is from the multiplying bunny triangle. Quote Link to comment Share on other sites More sharing options...
play_ Posted December 15, 2007 Author Share Posted December 15, 2007 Okay. So so far so good. But how would i get the parent folder? fro the URL? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 16, 2007 Share Posted December 16, 2007 well you can recover the url correct? Try this, and I haven't tested it yet, you will need to have .htaccess do some crazy stuff on it also <?php $errors = array(); $url = "http://mydomain.com/~username/folder1/folder2/folder3/folder4/image.jpg"; $path = explode("/",$url); $img_name = $path[count($path)-1]; $folder = $path[count($path)-2]; $parent_folder = $path[count($path)-3]; $user = $path[2]; //open sql connection $user = mysql_real_escape_string(trim(str_replace("~","",$user))); $q = "Select UserID from `USERTABLE` Where Username = '".$username."'" Limit 1"; $r = mysql_query($q) or die(mysql_error()); if(mysql_num_rows($r) >0){ $userid = mysql_result($r,0); } else{ $errors[] = "Invalid UserName"; } if(empty($errors)){ $q = "Select FolderID from `FOLDERTABLE` Where OwnerID = '".$userid."' && Name = '".$parent_folder."'"; $r = mysql_query($q) or die(mysql_error()); if(mysql_num_rows($r)>0){ $parent_possibles = array(); while($row = mysql_fetch_array($r)){ $parent_possibles[] = "Parent_ID = '".$row['FolderID']."'"; } } else{ $errors[] = "Invalid Folder Path."; } } if(empty($errors)){ $parents = implode(" || , ",$parent_possibles); $q = "Select FolderId from `FOLDERTABLE` Where Name = '".$folder."' && {".$parents."}"; $r = mysql_query($q) or die(mysql_error()); if(mysql_num_rows($r) >0){ $folder_id = mysql_result($r,0); $q = "Select ImageID from `IMAGETABLE` Where Name = '".$img_name."' && FolderID = '".$folder_id."'"; $r = mysql_query($q) or die(mysql_error()); if(mysql_num_rows($r) >0){ $img_id = mysql_result($r); echo "<img src=\"".$img_id.".jpg\" alt=\"\" />"; } else{ $errors[] = "Invalid Image Name."; } } else{ $errors = "Invalid Foder Path."; } } if(!empty($errors)){ echo "<ul>"; foreach($errors as $value){ echo "<li>".$value."</li>"; } echo "</ul>"; } ?> Its complex and sql can probably do something with joins to save you time, but it should work the thing I thought of while doing this was who cares if the image is associated to a user or not, its primary key from the mysql should be the image name, so that is what I did, getting that id is a bit complicated so this is what it does 1) Explode the url into subset parts to work on 2) Find that user's ID in the users table 3) Find all the possible parent folders that match that parent name and user id 4) From that list of parents we can determine the folderID of the folder it is in 5) Use that folderid and the image name to find the images id and display it I see an issue with what I did, it actualyl will need to run throug the full context and not just hte last two, but it gets you startred Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 16, 2007 Share Posted December 16, 2007 What you will need to do I just realized is take the folder contex into an array and then loop up frmo the root to the last folder and you always find the folder id of hte previous liek <?php $folders = array("Folder1","Folder2","Folder3"); $user = "test"; //Get id in a query from the username $q = "Select FolderID from `FOLDERTABLE` Where OwnerID = '".$userid."' and ParentID = '0'"; $r = mysql_query($q) or die(mysql_error()); if(mysql_num_rows($r) >0){ $previous = mysql_result($r,0); $i = 1; while($i <count($folders)-1){ $q = "Select `FolderID` From `FOLDERTABLE` Where Name = '".$folders[$i]."' and ParentID = '".$previous."'"; $r = mysql_query($q) or die(mysql_error()); $previous = mysql_result($r,0); $i++; } ?> after the loop $pervious is the folderID of hte iamge so query for an iamge with a name like that and that folder id and your good Quote Link to comment Share on other sites More sharing options...
play_ Posted December 16, 2007 Author Share Posted December 16, 2007 I haven't gotten that far yet. But thanks. I have this thread bookmarked. Will definitely repost here when i get that far. thanks cooldude! Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 17, 2007 Share Posted December 17, 2007 PM me if you get lost Quote Link to comment Share on other sites More sharing options...
play_ Posted December 17, 2007 Author Share Posted December 17, 2007 Thanks. going on vacation for three weeks this wednesday. So it might be a while. still, thanks for the immense help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.