Jump to content
Larry Ullman's Book Forums

Recommended Posts

For the list_dir.php script, line 42 sets conditions to determine if an item should be written or not.

 

The conditions are:

if ( (is_file($search_dir . '/' . $item)) AND ((substr($item, 0, 1) != '.') )

 

I rewrote the condition to fit my preferences, and I will be referring to my version when asking my question, so if my code is wrong that could be the problem. But to the best of my knowledge, they are equivalent.

if (is_file($search_dir . '/' . $item) && sub_str($item, 0, 1) != '.')

 

The conditions are quite obvious, and stated in the text. But when I execute the code, no files are shown, and I checked the HTML to make sure. It is the script. I began debugging and discovered that when I remove the second part of the condition

&& sub_str($item, 0, 1) != '.'

it works fine. That makes sense in a way, because it is less restrictive, but none of my file names begin with a period.

And what is odd is that I do not encounter a similar problem with directories. When the same 'second part' of the condition is removed for the directory section, the predicted outcome occurs (the parent '..' and current '.' directories appear).

 

I would like to point out that I am running my server off of my Linux (Ubuntu 11.10) computer - implying that I am not running Windows or Mac OS X. My webroot is the 'php' directory with this path:

/var/www/php/

Hypotheses:

  • A permissions issue
  • Code in the book is not applicable to my server's operating system
  • The php.ini file is configured differently than the author's

I do not know how to test any of the hypotheses, but I imagine I would be able to test the first one by using chmod, but I do not full understand what 'group' and 'other' mean, or what user PHP is under, or what permissions should be sufficient.

 

 

The entirety of my script follows.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="default.css" />
<title>Directory Contents</title>
</head>
<body>
<?php
// Set directory name and scan
$search_dir = '.';
$contents = scandir($search_dir);
// List directories
print '<h2>Directories</h2>
<ul>';
foreach ($contents as $item)
{
	if (is_dir($search_dir . '/' . $item) && substr($item, 0, 1) != '.')
	{
		print '<li>' . $item . '</li>' . "\n";
	}
}
print '</ul>';

// Create table for files
print '<h2>Files</h2>
<table>
<tr>
<td>Name</td>
<td>Size</td>
<td>Modified</td>
</tr>';
// List files
foreach ($contents as $item)
{
	if (is_file($search_dir . '/' . $item)) //  && (sub_str($item, 0, 1) != '.') was removed
	{
		$fs = filesize($search_dir . '/' . $item);
		$lm = date('F, j, Y', filemtime($search_dir . '/' . $item));
		print "
<tr>
<td>$item</td>
<td>$fs bytes</td>
<td>$lm</td>
</tr>";
	}
}
print '</table>';
?>
</body>
</html>

This HTML is the result of including the second part of the condition:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="default.css" />
<title>Directory Contents</title>
</head>
<body>
<h2>Directories</h2>
<ul><li>css</li>
<li>template</li>
</ul><h2>Files</h2>
<table>
<tr>
<td>Name</td>
<td>Size</td>
<td>Modified</td>
</tr>

I do not know why it does not print code after the foreach statement, or even after the closing php tag.

Link to comment
Share on other sites

// List files

foreach ($contents as $item)

{

if (is_file($search_dir . '/' . $item)) // && (sub_str($item, 0, 1) != '.') was removed

 

Would the problem be "was removed"? I suppose it was meant to be a comment?

 

I hope this helps,

Link to comment
Share on other sites

 

Would the problem be "was removed"? I suppose it was meant to be a comment?

 

I hope this helps,

The 'was removed' was not there when I was running the code. I am pretty sure the page would not have even loaded if the 'was removed' was present, so that is not the problem.

Link to comment
Share on other sites

There are only two differences between your conditional and mine:

- You've swapped AND for &&, which differs only in precedence.

- You repeatedly use sub_str(), which doesn't exist. The function is substr().

 

 

There's nothing about your OS that wouldn't work here. As for your PHP configuration, I gather display errors is not enabled or else you would see an error when you call sub_str().

 

Also, per the forum guidelines, you really ought to be including the version of PHP in use.

Link to comment
Share on other sites

 Share

×
×
  • Create New...