Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to arrays 1

Status
Not open for further replies.

chrismassey

Programmer
Aug 24, 2007
264
GB
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...

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
 
chrismassey,
Unlike perl, PHP uses $ notation for array type as well.
I have not gone through your code entirely but try changing just this part
Code:
$up_one_l = split("/", $dirurl);
$up_one_lb = array_pop($up_one_l);

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Spookie,

Thank you very much for your help. I tried changing that part and its worked exactly as I wanted it to. I have also found the link you provided very useful, and it will help me with future php coding.

Thanks again,

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top