Jump to content

[SOLVED] substr and readdir issue


dbol

Recommended Posts

Hello.

 

I've got a piece of code that almost works, minus 2 factors.

 

The first one has to do with with the substr method, and the second is a filemtime issue.

 

The code is as follows, and I will explain:

 

for($index=0; $index < $indexCount; $index++) {
    if (substr("$dirArray[$index]", 0, 1) != ".") || if (substr("$dirArray[$index]", -3, 3) != "ptn")) { 
        print("<TR><TD width=60%><a href=\"Z:/Clients/$clientid/$dirArray[$index]\">$dirArray[$index]</a></td>");
        print("<td width=16%><center><a href='Z:/Clients/$clientid/$dirArray[$index]'>[view]</a> <a href='insurance.php?action=deletefile&clientid=$clientid&file=$dirArray[$index]'>[remove]</a></center></td>");
        echo "<td width=40%><center>" . date('M d, Y', filemtime("D:\\Clients\\$clientid\\$dirArray[$index]")) . "</center></td>";
        print("</TR>\n");
    }		
}

 

What I'm doing here, is reading a directory using the readdir function, putting the elements into an array called $dirArray, and then cycling through a for loop and displaying all files in that directory. The problem here is, that it also displays hidden files, and my clients do not need to see them.

The 2nd line of code: if (substr("$dirArray[$index]", 0, 1) != ".") || if (substr("$dirArray[$index]", -3, 3) != "ptn"))

If I remove everything to the right of the OR (||), the code works fine, but I am trying to filter files that end with extension .ptn from displaying. As soon as I put that second substr back in, the page won't load properly. The second issue is with the filemtime function: echo "<td width=40%><center>" . date('M d, Y', filemtime("D:\\Clients\\$clientid\\$dirArray[$index]")) . "</center></td>";

I am trying to cycle through the files in the directory using that for loop, and print the filenames, along with their modified dates, however, the dates all show up as December 31, 1969. When I change the path to an exact path, it works fine, but won't work properly with $dirArray.

 

Just for sake of clarity, below is the while loop used to fill the $dirArray var.

 

while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}

Link to comment
https://forums.phpfreaks.com/topic/43868-solved-substr-and-readdir-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.