Page 1 of 1

PHP exploding directory tree

Posted: Sun Jun 21, 2009 3:46 pm
by St1ckman
Hello,
I have been messing about with php for a couple of years now, just on and off, mostly dabbling with a lightweight uploading script I made. This has been bothering me for some time so I thought I would ask here for some tips!
I have made a directory browsing script, that allows you to select a directory to upload to, using the GET function.
Here's the script: ($directory is the directory chosen for uploading to, $multiupload is just to select how many upload forms you want)

Code: Select all

 
<?php 
$directory = @$_GET['folder'];
$multiupload = @$_GET['multiup'];   
$direx = explode('/', $directory);
$direxcount = count($direx);
$direxreplaced = str_replace('/'.$direx[$direxcount-1], '', $directory);
$opendir1=opendir('./');
while (false != ($filefolder = readdir($opendir1))){
    if ($filefolder != '.' && $filefolder != '..' && $filefolder != 'cfg.php' && $filefolder != 'index.php') {
    if (is_dir($filefolder)){
if ($filefolder == $direx[0]){ echo $directory.'/<BR>';}else{
echo('<a href="?folder='.$filefolder.'&mutliup='.$multiupload.'">'.$filefolder.'/</a><BR>'); }
if ($filefolder == $direx[0]){
 
if ($direxcount > 1){
 
echo '< <a href="?folder='.$direxreplaced.'&multiup='.$multiupload.'">Back to: '.$direxreplaced.'</a><BR>';}}
$opendir2 = @opendir($directory);
while (false != ($folder2 = @readdir($opendir2))) {
    if (!is_dir($folder2)) {
$ext2 = strrchr($directory.'/'.$folder2, '.');
$ext2 = strtolower(substr($ext2, 1));   
$finf = array($folder2);
$len2 = count($finf);
for ($i2 = 0; $i2 < $len2; $i2++) {
if ($ext2 == ""){
 
if ($filefolder == $direx[0]){
 
echo '<a href="?folder='.$directory.'/'.$finf[$i2].'">/'.$finf[$i2].'</a><BR>';
$subfolder = 1;
}}}}}echo '<BR>';
//end of opendir1
}}}
//end of opendir2
?>
 
This displays something like this:
Stickman/Images/
< Back to: Stickman
/Design
/Work
/Photos
/Random
/Screenshots

This code is very sloppy, it will grab the main directories and plonk them in a list, that I want people to upload to.
Then it keeps that structure, and places the sub directories from the main folder selected, under the main folder in the list. ( I could have it pop to the top instead, but I find that confusing )
Problem is, it sticks the main folder at the start of the link, and the subdirectories in one long string so, it makes (1)$directory.(2)$finf[$i2] like this (1)Stickman/(2)Images/Random/funny
So as for the link "< Back to: [Foldername]" I made something which I knew how to do, which was just to explode the directories and remove the last name off the end using the explode array to display a 'Go back to the last folder' Sort of thing...
What I wanted was to have a link for each sub-directory all the way back to the main directory, instead of just a 'go back one' link... Any help on improving this would make my day haha, if you need any more code let me know, or even the full page.

Thanks,
Stick

PS Is there any way to time an upload, so that once an upload button is pressed it sets a timer off and stop once all files are uploaded?

Re: PHP exploding directory tree

Posted: Mon Jun 22, 2009 7:43 am
by patrickmvi
It sounds to me like you'd want to store your current location in some sort of session based array that you could then easily refer to for your breadcrumbs you're trying to create. For instance, is you were in this folder path:

/folderone/foldertwo/folderthree

you would have an array that would look like array(0 => "folderone", 1 => "foldertwo", 2 => "folderthree"). Then when you're displaying your breadcrumbs, you could have a get variable that would reference the index (0,1,2) and based on that, you would know which folder to "jump back" to, then you'd remove any additional items from your array. So if you went from folderthree to foldertwo, you would pass 1 via get and then you would look up the folder based on your session array and then you would drop anything higher than 1 from your breadcrumb array. Depending on how you're doing this, you may need to store more information in your breadcrumb array than just the folder name. If so, use a multi-dimension array. Hopefully this makes some sense.

Re: PHP exploding directory tree

Posted: Tue Jun 23, 2009 4:04 pm
by St1ckman
Thanks for the reply patrickmvi.
That made me hungry..

I'll see what I can do about the session, I thought about it before, but then I couldn't think of how I could stick the folders back together one after another, removing one at a time to go back to the root directory.
Got a couple days work to do, then I'll try again, probably Thursday evening.
Thanks again,
Stick

Re: PHP exploding directory tree

Posted: Tue Jun 30, 2009 2:01 pm
by St1ckman
Well, not having luck, thanks though, I'll probably have to work off someone elses directory tree to better understand it.
Sorry for the delay, I was out all weekend.
If I can be arsed to change it, I'll let you know haha.

Re: PHP exploding directory tree

Posted: Wed Jul 01, 2009 6:24 pm
by St1ckman
To simplify this, could someone show me how I could take this segment:

One/Two/Three/Four/Five

and have php push out:

One
One/Two
One/Two/Three
One/Two/Three/Four
One/Two/Three/Four/Five

Not limited to five,
Thanks,
St1ckman

Re: PHP exploding directory tree

Posted: Sun Jul 05, 2009 7:18 am
by youlikethaaaat
St1ckman wrote:To simplify this, could someone show me how I could take this segment:

One/Two/Three/Four/Five

and have php push out:

One
One/Two
One/Two/Three
One/Two/Three/Four
One/Two/Three/Four/Five

Not limited to five,
Thanks,
St1ckman

Code: Select all

<?php
    $path = "One/Two/Three/Four/Five";
    $path = explode("/",$path);
    foreach($path as $dir) {
        $str .= "/".$dir;
        echo substr($str,1)."<br>";
    }
?>

Re: PHP exploding directory tree

Posted: Thu Jul 09, 2009 5:22 pm
by St1ckman
youlikethaaaat wrote:

Code: Select all

<?php
    $path = "One/Two/Three/Four/Five";
    $path = explode("/",$path);
    foreach($path as $dir) {
        $str .= "/".$dir;
        echo substr($str,1)."<br>";
    }
?>
You sir, are an absolute legend. That works perfect, and makes my directory structure much more versatile, and simple!
My minds at ease now, thanks!