Zend - The PHP Company




Files and Directories

Add Code


File & Folder Picker / Selector for PHP  

Type: code fragment
Added by: CHnuschti
Entered: 21/10/2003
Last modified: 02/11/2002
Rating: - (fewer than 3 votes)
Views: 9864
Hi. I wrote this one for "local" purposes. It allows to navigate and pick up an unlimited amount of files and folders. It returns a 2-dim-array with the file/folderpath and an index: 1=folder, 2=file. It requires javascript. It was tested "local" on Windows2000 and IE6, but not on an "external" server nore with other browsers. You'll have to adapt the list of the drives manually.


<html><body>

<?php

// function list any array
// use: get_array_elements($my_array, $arrayname);, where $my_array=the_real_array and $arrayname="my_array" e.g. .The list will be with chosen $arrayname
// alternative use with ($my_array, "my_array");

function get_array_elements($realarray$arrayname)

while(list(
$key,$value)=each($realarray))
    { 
    if (
is_array($value)) get_array_elements($value$arrayname."[$key]"); 
    else for (
$i=0$i<count($value);$i++) echo $arrayname."[$key]=".$value."<BR>n"
    } 


// function send any array as hidden in a form with post-methode, same use as above. Note: Array will be sent with provided $arrayname

function post_hidden_array($realarray$arrayname)

while(list(
$key,$value)=each($realarray))
    { 
    if (
is_array($value)) post_hidden_array($value$arrayname."[$key]"); 
    else for (
$i=0$i<count($value);$i++) echo "<input type="hidden" name="" . $arrayname . "[$key]" . "" value="" . $value . "">";
    } 


//****************************************************************************

//make all variables available
foreach($_POST as $k => $v) $$k=$v;    

if (!isset(
$status)) $status=0;
?>


<?php
//PAGE 0
if ($status==0)
{

echo 
"<b>THIS IS PAGE 0</b><br>";
echo 
"**************<br>";


//explode selection to an array: $selection[xy][0]=file/folderpath; $selection[xy][1]=index: 1=folder, 2=file
if (isset($selection))
{
for (
$xx12=0$xx12<count($selection); $xx12++)
$selection[$xx12]=explode(",",$selection[$xx12]);
}

//list selection
if (isset($selection)) get_array_elements($selection"selection");
?>

<form action="<?php $PHP_SELF ?>" method = "post">
<input type="hidden" name="status" value="1">
<input type="submit" value="select files">
</form>

<?php
}
?>





<?php
//PAGE 1
if ($status==1)
{
echo 
"<b>THIS IS PAGE 1</b><br>";
echo 
"**************<br>";

//list of drives: has to be written manually
$xx19=array("a:""b:""c:""d:""e:""f:""g:""h:""i:""j:",);

// default path for first run
$xx1="c:/programme";

//make all variables available
foreach($_POST as $k => $v) $$k=$v;

//add previous picked to selection
for ($xx13=0$xx13<count($selectstep); $xx13++)
$selection[]=$selectstep[$xx13];

//and explode selection for display
for ($xx15=0$xx15<count($selection); $xx15++)
$xx16[$xx15]=explode(",",$selection[$xx15]);

//display already chosen
echo "Selected so long:<br>";
if (isset(
$xx16))
{
echo 
"<b>FOLDERS:</b><br>";
for (
$xx17=0$xx17<count($xx16); $xx17++) 
if (
$xx16[$xx17][1]==1) echo $xx16[$xx17][0] . "<br>";
echo 
"<b>FILES:</b><br>";
for (
$xx18=0$xx18<count($xx16); $xx18++) 
if (
$xx16[$xx18][1]==2) echo $xx16[$xx18][0] . "<br>";
}
else echo 
"nothing<br>";
echo 
"******************************************************<br>";

//set paths, read paths
if (substr($xx1strlen($xx1)-11)=="/"$xx1=substr($xx1,0,strlen($xx1)-1);
if (
$navigation==1$xx1=$xx2;            //set path once chosen
$xx11=substr($xx10strrpos($xx1"/"));    //this is the path up one level

$xx3 opendir($xx1);    //opens selected path
while (false !== ($xx5 readdir ($xx3)))
{
$xx4++;
if (
is_dir($xx1 "/" $xx5) && $xx5 != "."$folders[]=$xx1 "/" $xx5;
if (
is_file($xx1 "/" $xx5)) $files[]=$xx1 "/" $xx5;
}
?>


<!-- display drives, folder, pickarea -->

Navigate on the left, pick files & folders on the right (ctrl+mouseclick for multiple pick)<br>
Current location: <?php echo $xx1?><br>

<table border style="border-collapse:collapse";>
<tr><td> <b>DRIVES:</b> </td><td> <b>FOLDERS:</b> </td><td> <b>PICK FILES & FOLDERS HERE</b> </td></tr>

<tr><td valign="top">

<?php

//display drives
for ($xx20=0$xx20<count($xx19); $xx20++)
echo 
"<a href="#" onclick="document.forwardlink.submit(document.forwardlink.xx2.value = '" . $xx19[$xx20] . "');">" . $xx19[$xx20] . "</a><br>";

?>

</td><td valign="top">

<?php

//display folders
for ($xx8=0$xx8<count($folders); $xx8++)
{
if (
substr($folders[$xx8],strrpos($folders[$xx8],"/")+1) == "..")
echo 
"<a href="#" onclick="document.forwardlink.submit(document.forwardlink.xx2.value = '" . $xx11 . "')">UP ONE LEVEL</a><br>";

else
echo 
"<a href="#" onclick="document.forwardlink.submit(document.forwardlink.xx2.value = '" . $folders[$xx8] . "')">" . substr($folders[$xx8],strrpos($folders[$xx8],"/")+1) . "</a><br>";
}

?>

</td>

<!-- form navigation -->

<form name="forwardlink" action="<?php $PHP_SELF ?>" method = "post">

<?php
//send selection
if (isset($selection)) post_hidden_array($selection"selection");
?>

<input type="hidden" name="xx2" value="<?php echo $xx2?>">
<input type="hidden" name="navigation" value="1">
<input type="hidden" name="status" value="1">

</form>

<!-- display pickarea -->

<td valign="top">

<form action="<?php $PHP_SELF ?>" method = "post">

<?php
//calculate size of window
if (substr($xx1,strlen($xx1)-1,1)==":"$xx21=$xx4+2; else $xx21=$xx4;
?>

<select name="selectstep[]" size="<?php echo $xx21?>" multiple>

<optgroup label="FOLDERS:">

<?php
for ($xx9=0$xx9<count($folders); $xx9++)
if (
substr($folders[$xx9],strrpos($folders[$xx9],"/")+1) != "..")
echo 
"<option value="" . $folders[$xx9] . ",1" . "">" substr($folders[$xx9],strrpos($folders[$xx9],"/")+1) . "</option>";
?>

</optgroup>

<optgroup label="FILES:">

<?php
for ($xx10=0$xx10<count($files); $xx10++)
echo 
"<option value="" . $files[$xx10] . ",2" . "">" substr($files[$xx10],strrpos($files[$xx10],"/")+1) . "</option>";
?>

</optgroup>

</select>

</td></tr></table>

<!-- form pick files&folders -->

<?php
//send selection
if (isset($selection)) post_hidden_array($selection"selection");
?>

<input type="hidden" name="xx2" value="<?php echo $xx1?>">
<input type="hidden" name="navigation" value="1">
<input type="hidden" name="status" value="1">
<input type="submit" value="pick selected">

</form>

<!-- delete selection -->

<form action="<?php $PHP_SELF ?>" method = "post">

<input type="hidden" name="xx2" value="<?php echo $xx1?>">
<input type="hidden" name="navigation" value="1">
<input type="hidden" name="status" value="1">
<input type="submit" value="delete selection (reset)">

</form>

<!-- form finish and go back -->

<form action="<?php $PHP_SELF ?>" method = "post">

<?php
//send selection
if (isset($selection)) post_hidden_array($selection"selection");
?>

<input type="hidden" name="status" value="0">
<input type="submit" value="terminated, back to page 0">

</form>

<?php
}
?>

</body></html>


Usage Example


The code comes as a complete example. Save and run it to see how it works. You'll have to refine it if you want to integrate it in your script.


Rate This Script





Search



This Category All Categories