Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Task 3B-2
March 03, 2008 07:51AM
Done
Re: Task 3B-2
March 04, 2008 08:33AM
completed
Re: Task 3B-2
March 15, 2008 01:43PM
I have done both exercises under 3B-1. But now I am having trouble with 3B-2. I have tried so many diffirent things and I keep getting an error for line 19 of my code which is: if($item != '.' && $item != '..'winking smiley The error message is Parse error: parse error, unexpected T_IS_NOT_EQUAL

i have read over it so many times now, tried writing it differently but seriously dont know what im doing wrong... Any ideas?
avatar
Mac
Re: Task 3B-2
March 16, 2008 05:58PM
Post more code than this....the error may have started way earlier....

Do you have $item=$_POST['item'];or use $_POST['item']
Anonymous User
Re: Task 3B-2
March 16, 2008 06:48PM
Done
Re: Task 3B-2
March 17, 2008 07:00AM
Hey Mac

please see my code below but I have no idea why or where i would put $item=$_POST['item'], please explain, I am lost.

--------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
<html xmlns="http://www.w3.org/1999/xhtml">;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
/* This script creates a directory navigation script that allows users
to view the contents of a directory as well as the contents
of the files within the directory.*/

if(!$dir)
$dir=("./"winking smiley;
$dp=opendir($dir);

echo "<table border='0' width ='100%' cellspacing='0' cellpadding='0'>\n";
while($filenames[] = readdir($dp));

sort($filenames);
for($i=0; $i < count($filenames); $i++) {
$item = $filenames[$i];

if(is_dir($item))
{
if($item != '.' && $item != '..'winking smiley

echo "<tr><td width ='100%'><a href='$PHP_SELF?dir=$dir$item'>$item/</a></td></tr>\n";
}
else
echo "<tr><td width='100%'><a href='$dir/$item'>$item</a></td></tr>\n";
}
?>
</table>
</body>
</html>
avatar
Mac
Re: Task 3B-2
March 18, 2008 04:39PM
Ah, I answered forgetting what 3b-2 is.... sorry.

This code is 100% and works perfectly? It generates a directory listing for me with no error?

Try running this code:
Language: PHP
<?php // show directory content function showDir($dir, $i, $maxDepth){ $i++; if($checkDir = opendir($dir)){ $cDir = 0; $cFile = 0; // check all files in $dir, add to array listDir or listFile while($file = readdir($checkDir)){ if($file != "." && $file != ".."){ if(is_dir($dir . "/" . $file)){ $listDir[$cDir] = $file; $cDir++; } else{ $listFile[$cFile] = $file; $cFile++; } } }   // show directories if(count($listDir) > 0){ sort($listDir); for($j = 0; $j < count($listDir); $j++){ echo " <tr>"; $spacer = ""; for($l = 0; $l < $i; $l++) $spacer .= "&emsp;"; // create link $link = "<a href=\"" . $_SERVER["PHP_SELF"] . "?dir=" . $dir . "/" . $listDir[$j] . "\">$listDir[$j]</a>"; echo "<td>" . $spacer . $link . "</td> </tr>"; // list all subdirectories up to maxDepth if($i < $maxDepth) showDir($dir . "/" . $listDir[$j], $i, $maxDepth); } }   // show files if(count($listFile) > 0){ sort($listFile); for($k = 0; $k < count($listFile); $k++){ $spacer = ""; for($l = 0; $l < $i; $l++) $spacer .= "&emsp;"; echo " <tr> <td>" . $spacer . $listFile[$k] . "</td> </tr>"; } } closedir($checkDir); } }   if($_GET["dir"] == "" || !is_dir($_GET["dir"])) $dir = getcwd(); else $dir = $_GET["dir"]; // replace backslashes, not necessary, but better to look at $dir = str_replace("\\", "/", $dir);   // show parent path $pDir = pathinfo($dir); $parentDir = $pDir["dirname"];   echo "<a href=\"" . $_SERVER["PHP_SELF"] . "\"><h3>Home</h3></a>"; echo "Current directory: " . $dir; echo "<a href=\"" . $_SERVER["PHP_SELF"] . "?dir=$parentDir\"><h4>Parent directory: $parentDir</h4></a>";   // Display directory content echo"<table border=1 cellspacing=0 cellpadding=2> <tr><th align=left>File / Dir</th>";   // specifies the maxDepth of included subdirectories // set maxDepth to 0 if u want to display the current directory $maxDepth = 0; showDir($dir, -1, $maxDepth);   ?>

Your real problem answered later.
Re: Task 3B-2
March 19, 2008 08:47AM
Hey Mac

I tried my code again and it WORKED!!! Can't believe it, extremely WEIRD! Thank-you though.
JwV
Re: Task 3B-2
March 26, 2008 10:04AM
Hi all

I need help.

I entered to code correctly for this task, yet when run, the program does not want to display the contents of the sub directories. When I click on a sub folder it displays "http://127.0.0.1/PHP/dir_nav.php?dir=./Test"; in the address bar of the browser and does nothing else. When I click on any of the documents it runs the php/html files and displays the contents of the txt files. I ran the php script listed by Mac and that works. Am running this under php5. Tested it under php4 and got the same results. Have tried various changes to the echo statement but with no luck.

My script is as follows:

/* This script creates a directory navigation script that
allows users to view the contents of a directory as well
as the contents of the files within the directory. */

if (!$dir)
$dir = ("./"winking smiley;

$dp = opendir($dir);
if(!$dp) die ("directory not opened"winking smiley;

echo "<table border='0' width='100%' cellspacing='0' cellpadding='0'>\n";
while ($filenames[] = readdir($dp));

sort($filenames);
for($i = 0; $i < count($filenames); $i++)
{
$item = $filenames[$i];
if (is_dir($item))
{
if ($item != '.' && $item != '..'winking smiley
echo "<tr><td width='100%'><a href='$PHP_SELF?dir=$dir$item'>$item/</a></td></tr>\n";
//echo "<tr><td width='100%'><a href='$PHP_SELF$dir$item/'>$item/</a></td></tr>\n";
//echo "<tr><td width='100%'><a href='$dir$item'>$item/</a></td></tr>\n";
}
else
echo "<tr><td width='100%'><a href='$dir/$item'>$item</a></td></tr>\n ";
}
?>
</table>

The html source generated by php is as follows : (first few lines of source)

<table border='0' width='100%' cellspacing='0' cellpadding='0'>
<tr><td width='100%'><a href='.//'></a></td></tr>
<tr><td width='100%'><a href='?dir=./Network'>Network/</a></td></tr>
<tr><td width='100%'><a href='?dir=./Test'>Test/</a></td></tr>
<tr><td width='100%'><a href='.//addrecord_form.php'>addrecord_form.php</a></td></tr>
<tr><td width='100%'><a href='.//array.php'>array.php</a></td></tr>

Hope someone can help.

Thanks
avatar
Mac
Re: Task 3B-2
March 27, 2008 08:16AM
$dir=$_GET['dir'];
at the top
JwV
Re: Task 3B-2
March 27, 2008 08:56AM
Thanks Mac. Its working now.
Re: Task 3B-2
March 30, 2008 11:58AM
I completed this a while back I just forgot to post it...sorry sad smiley
Re: Task 3B-2
April 28, 2008 01:46AM
I completed it a while back as well and only posting now
Re: Task 3B-2
May 13, 2008 10:10PM
Completed a while back, just a bit slow in posting
Sorry, only registered users may post in this forum.

Click here to login