ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have this small portion of code That doesn't seem to work because of the the directory SCIENCE. I mean I don't think its because the directory is SCIENCE but because I am using the directory name in the code. I was hoping someone might be able to tell me whats wrong with the code by looking at it.
Code:
Here are the error messages I get only when I use the directory name in the code below.
Warning: filemtime(): stat failed for The Moon in C:\Apache2.2\htdocs\forum\index.php on line 154
Warning: filemtime(): stat failed for The Sun in C:\Apache2.2\htdocs\forum\index.php on line 154
Warning: filemtime(): stat failed for new_topic.html in C:\Apache2.2\htdocs\forum\index.php on line 154
Code:
This code doesn't work correctly.
<?php
// open this directory
$myDirectory = opendir("SCIENCE");
// get each entry
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
// close directory
closedir($myDirectory);
// count elements in array
$indexCount = count($dirArray);
// sort 'em
sort($dirArray);
array_multisort(array_map('filemtime', $dirArray), SORT_ASC, $dirArray);
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++)
{
if (substr("$dirArray[$index]", 0, 1) != "."
&& $dirArray[$index] != "index.php"
&& $dirArray[$index] != "new_topic.html")
{
print("<br> <a href=\"SCIENCE/$dirArray[$index]\">$dirArray[$index]</a> ");
print("is a ".filetype("SCIENCE/".$dirArray[$index]).", ");
print("its size is ".filesize("SCIENCE/".$dirArray[$index])." byte");
if(filesize("SCIENCE/".$dirArray[$index]) != 1)
print("s");
}
}
?>
Code:
This code works correctly.
<?php
// open this directory
$myDirectory = opendir("./");
// get each entry
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
// close directory
closedir($myDirectory);
// count elements in array
$indexCount = count($dirArray);
// sort 'em
sort($dirArray);
array_multisort(array_map('filemtime', $dirArray), SORT_ASC, $dirArray);
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++)
{
if (substr("$dirArray[$index]", 0, 1) != "."
&& $dirArray[$index] != "index.php"
&& $dirArray[$index] != "new_topic.html")
{
print("<br> <a href=\"$dirArray[$index]\">$dirArray[$index]</a> ");
print("is a ".filetype($dirArray[$index]).", ");
print("its size is ".filesize($dirArray[$index])." byte");
if(filesize($dirArray[$index]) != 1)
print("s");
}
}
?>
Also the code below I think is the cause of the problem cause a normal sort works but that code below I can't get to work with the directory in the code.
That trick didn't work. And its weird because the top code doesn't work and the bottom does and if I delete science and just use it in it's current directory then it works fine. I've tried adding ./ before SCIENCE here and it didn't work.
Code:
Doesn't work
print("<br> <a href=\"SCIENCE/$dirArray[$index]\">$dirArray[$index]</a> ");
print("is a ".filetype("SCIENCE/".$dirArray[$index]).", ");
print("its size is ".filesize("SCIENCE/".$dirArray[$index])." byte");
if(filesize("SCIENCE/".$dirArray[$index]) != 1)
print("s");
Code:
Works
print("<br> <a href=\"$dirArray[$index]\">$dirArray[$index]</a> ");
print("is a ".filetype($dirArray[$index]).", ");
print("its size is ".filesize($dirArray[$index])." byte");
if(filesize($dirArray[$index]) != 1)
print("s");
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.