Jump to content

Need just a little bit of help with this line of code


$Three3

Recommended Posts

Hey everyone, I have been reading a php book and following all of the examples in the book but I am stuck on this one line. I actually understand the hardest part of this script but not the simple part. Here is the code:

 

<?php

date_default_timezone_set('US/Central') ;

//Scan directories
$searchDirectory = '.' ;
$directoryScan = scandir($searchDirectory) ;

//Create a list of directories first
echo "<h2>Directories</h2>" ;

echo "<ul>" ;

//Access the contents of the directory
foreach ($directoryScan as $contents) {
if ((is_dir($contents)) AND (substr($contents , 0 , 1) != '.')) {
echo "<li>$contents</li>" ;
}
}

echo "</ul>" ;

//Create Files header
echo "<hr /><h1>Files</h1>" ;

//Create Table
echo '<table cellpadding="2" cellspacing="10" align="left">' ;
//Create Files Row
echo "<tr>
	 <td><b><u>Name</u></b></td>
	  <td><b><u>Size</u></b></td>
	  <td><b><u>Last Modified</u></b></td>
	  <td><b><u>File Permissions</u></b></td>
	  <td><b><u>File Owner</u></b></td>
	  <td><b><u>File Last Accessed</u></b></td>
 </tr>" ;

//Access the files in the directory
foreach ($directoryScan as $contents) {
if ((is_file($contents)) AND (substr($contents, 0 , 1) != '.')) {
	//Create File Size & Last Mod date
	$fs = filesize($contents) ;
	$lm = date('F j, Y' , filemtime($contents)) ;
	$fp = fileperms($contents) ;
	$fo = fileowner($contents) ;
	$fla = date('F j, Y' , fileatime($contents)) ;
	//Print Out Data
	echo "<tr>
		      <td>$contents</td>
			  <td>$fs</td>
			  <td>$lm</td>
			  <td>$fp</td>
			  <td>$fo</td>
			  <td>$fla</td>
		  </tr>" ;
}
}

echo "</table>" ;

?>

 

What is confusing me is this line:

 

if ((is_dir($contents)) AND (substr($contents , 0 , 1) != '.')) {

 

I understand that whole line but what I do not understand is the part with the '.' period. What does this mean? Thanks in advance for the help.

Link to comment
Share on other sites

Well $contents is the name of a file or folder within the directory. The part of the if which is substr($contents, 0, 1) != '.' is simply saying if the first character in $contents is not a dot/period.  The '.' is just a PHP string containing a dot, nothing magical or special.

Link to comment
Share on other sites

Well $contents is the name of a file or folder within the directory. The part of the if which is substr($contents, 0, 1) != '.' is simply saying if the first character in $contents is not a dot/period.  The '.' is just a PHP string containing a dot, nothing magical or special.

 

Hey thanks for the reply. Okay I understand that now but what I do not understand is why you would want to make sure that variable $contents is not equal to a dot/period? Thanks again man

Link to comment
Share on other sites

The contents of the folder will have special traversal "folders" called . and .. (for current and parent folders respectively) and also "hidden" files are commonly a prefixed with a dot.  Take a look at the scandir manual page.

 

Awesome. I understand now. Thanks a lot for that link and for your time and help. Appreciate it

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.