Get function for directorys using glob/ opendir

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
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Get function for directorys using glob/ opendir

Post by ozzy »

Hey,

im wanting to create a directory indexer, but my problem is when the directory is indexed it is indexed directly to the directory and im not wanting to put the same script in every directory. Is there any way i can use pgp GET to index the directory within the index.php (hope yee understand). So the directorys can be accessed like:

Code: Select all

www.EXAMPLE.net/index.php?dir=TEST DIRECTORY
Instead of:

Code: Select all

www.EXAMPLE.net/TEST DIRECTORY/
Example of glob:

Code: Select all

<?php
foreach (glob("*.txt") as $filename) {
   echo "$filename size " . filesize($filename) . "\n";
}
?>
Thanks in advance!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

sure...just use your $_GET var for the directory name.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Thanks, I understand the concept of it, but i can seem to gte it working with my existing script:

Code: Select all

<?php

$location = "Library";  
$dir = getcwd().'/';
$difference = 30; 
$start = isset($_GET['start']) ? (int)$_GET['start'] : 0; 
$end = isset($_GET['end']) ? (int)$_GET['end'] : 0; 
$size = count($file_array["name"]);
if($end <= $start) { 
    $end = $start+30; 

} 



function smart_filemtime($filename) { 

  $time = @filemtime($filename); 

  if ($time) return $time; 

  else return time(); 

}   

if (is_dir($dir)) 

{ 

    $i = 1; 

    $files = glob($dir . 'Library/*');

foreach($files as $file)

{

  $file_array["name"][] = basename($file);   

  $file_array["date"][] = filemtime($file);

}

array_multisort($file_array["date"], SORT_NUMERIC,SORT_DESC, $file_array["name"]); 

//how many blocks?

$nb = count($file_array["name"]);

$nbOfBlock = intval($nb / $difference);

if(($nb % $difference) ==  0)

    $nbOfBlock--;


//display from where to where ?

if($_GET["p"])

$minDisplay = ($_GET["p"] - 1) * $difference;

else

    $minDisplay = 0;
if(($minDisplay + $difference) > $nb)
    $maxDisplay = $nb;
else
    $maxDisplay = $minDisplay + $difference;

//print "min : $minDisplay max : $maxDisplay";

//display the right block

 ?>

 <div align = "center">

<?php

//pagination     

//further for the links of ps Smile

if(++$nbOfBlock)

{

    for($i = 1;$i <= $nbOfBlock;$i++)

        print " - <a href=\"?name=Library&file=index&p=$i\">$i</a>";

}

echo " - ";

?>

</div>

<?php

    for($i = $minDisplay;$i < $maxDisplay; $i++)   

    { 

        echo "<tr style='border:1px solid #cfcfcf;background:#fcfcfc; cursor:hand\' onMouseOver=\"this.style.background='#EEEEEE'\" onMouseOut=\"this.style.background='#FFFFFF'\" onClick=\"window.location.href='modules.php?name=Library&file=file&page=".$file_array["name"][$i]."'\"> 

        <td width=\"70%\"><a href=\"modules.php?name=Library&file=file&page=".$file_array["name"][$i]."\">".$file_array["name"][$i]."</a> 

        </td> 

        <td width=\"30%\" align=right><a href=\"$location".$file_array["name"][$i]."\">Plaintext</a> | <i>".$time.date ("F d Y", $file_array[date][$i]); 

        echo "</i></td>"; 

    } 

} 
?>



</table>

<div align = "center">

<?php

if($nbOfBlock)

{
    for($i = 1;$i <= $nbOfBlock;$i++)

        print " - <a href=\"".$PHP_SELF."?name=Library&file=index&p=$i\">$i</a>";

}

echo " - ";

?>
I have tryed replacing

Code: Select all

$dir = getcwd().'/';
with

Code: Select all

$dir = $_GET['dir'].'/';


But then i get the followign error:

Code: Select all

Warning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in /host/localhost/htdocs/modules/Library/index.php on line 44
I have tryed commenting out this line, but then nothing is displayed. I think something in the script is cntradicting something else, but im not sure why.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try echoing out the $_GET var plus your *.* to make sure it's giving you what you think it is.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Right, i got the first part

Code: Select all

plus your *.* to make sure it's giving you what you think it is.
But i dont quite follow the above. :?

I have tryed replacing

Code: Select all

$files = glob($dir . 'Library/*');
with

Code: Select all

$files = glob($dir . 'Library/$dir*');
But im still faced with the array_multisort() error and with no files being indexed.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

so try what I suggested and echo it out to make sure it's what you think it is:

Code: Select all

$dir = getcwd().'/';
$dir2 = $_GET['dir'].'/';
echo 'old way '.$dir;
echo 'new way '.$dir2;
$files1 = glob($dir.'*.*');
print_r($files1);
$files2 = glob($dir2.'*.*');
print_r($files2);
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Ah, i unserstand now, but im wanting to get working along side my current script but whenever i try the phandom error message always gets displayed:

Code: Select all

Warning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in /host/localhost/htdocs/modules/Library/index.php on line 44
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

that error means that glob() isn't returning an array. did you try what I suggested? if so, what did it show?
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

I did what you suggested, and i got it to work, although im wanting it to work alongside with my current script but i cant seem to do that due to the error.
Post Reply