chrismassey
Programmer
Hello,
I have only just started writing PHP today, but am familiar with other programming languages such as perl.
I am trying to build a simple webspace explorer system. Simply it reads a directory and displays the contents, if file then once clicked it opens file in new window, and if directory then it will open the new directory and display its contents in the same manner etc etc.
Now, I want a button which allows me to move up one level. My idea was to take the string containing the URL, split it into pieces using / as the split character, pop off the last element into a scaler variable (current directory), then use str replace to remove the last variable from the original URL, so that the URL is now 1 directory above than it previously was.
However, the file system may be of any size therefore there could be 1 - 100 levels, and therefore there is no set size for the array. In perl for example an array can be displayed as @array, and all the data passed into there.
In PHP I haven't found a method to manipulate an array so it can vary in size (number of items).
I hope this explains what I am trying to do, but here is my code anyway...
Thanks alot,
Chris
I have only just started writing PHP today, but am familiar with other programming languages such as perl.
I am trying to build a simple webspace explorer system. Simply it reads a directory and displays the contents, if file then once clicked it opens file in new window, and if directory then it will open the new directory and display its contents in the same manner etc etc.
Now, I want a button which allows me to move up one level. My idea was to take the string containing the URL, split it into pieces using / as the split character, pop off the last element into a scaler variable (current directory), then use str replace to remove the last variable from the original URL, so that the URL is now 1 directory above than it previously was.
However, the file system may be of any size therefore there could be 1 - 100 levels, and therefore there is no set size for the array. In perl for example an array can be displayed as @array, and all the data passed into there.
In PHP I haven't found a method to manipulate an array so it can vary in size (number of items).
I hope this explains what I am trying to do, but here is my code anyway...
Code:
<?
$i_path = '/PHP/Examples/File_Access/Read_Directory_Contents/Files';
$i_url = '/PHP/Examples/File_Access/Read_Directory_Contents/Files';
$script_url = 'Read_Directory_Contents2.php';
$file_carry = $_GET['file'];
$dirpath = "$i_path/$file_carry"; $dirpath = str_replace('//', '/', $dirpath);
$dirurl = "$i_url/$file_carry"; $dirurl = str_replace('//', '/', $dirurl);
$dirurl = str_replace('http:/', '[URL unfurl="true"]http://',[/URL] $dirurl);
// THIS IS THE SECTION I AM TRYING TO FIX.
@up_one_l = split("/", $dirurl);
$up_one_lb = array_pop(@up_one_l);
$dirurl = str_replace("/$up_one_lb", "", $dirurl);
echo "<b>$dirurl</b><p>";
echo "<a href=\"$script_url\">Top Level</a>";
echo ' | ';
echo "<a href=\"$script_url?file=$up_one_l\">Up One Level</a><p>";
$counter = 0;
if ($handle = opendir($dirpath)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir("$dirpath/$file")) {
// Directory
echo "<a href=\"$script_url?file=$file_carry/$file\"><b>[$file]</b></a><br>";
$counter++;
}
else {
// File
echo "<a href=\"$dirurl/$file\" target=\"_blank\">$file</a><br>";
$counter++;
}
}
}
closedir($handle);
}
if ($counter == 0) {
echo 'Directory Is Empty.';
}
?>
Thanks alot,
Chris