LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2012, 01:58 AM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
Cool php syntax question


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>&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"SCIENCE/$dirArray[$index]\">$dirArray[$index]</a>&nbsp;");
		print("is a ".filetype("SCIENCE/".$dirArray[$index]).",&nbsp;");
		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>&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"$dirArray[$index]\">$dirArray[$index]</a>&nbsp;");
		print("is a ".filetype($dirArray[$index]).",&nbsp;");
		print("its size is ".filesize($dirArray[$index])." byte");
		if(filesize($dirArray[$index]) != 1)
			print("s");
	}
}
?>
 
Old 01-13-2012, 02:20 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Examine (ls -l, stat, getfacl) the files in question: "SCIENCE/The Moon", "SCIENCE/The Sun", "SCIENCE/new_topic.html"
 
Old 01-13-2012, 02:24 AM   #3
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by NevemTeve View Post
Examine (ls -l, stat, getfacl) the files in question: "SCIENCE/The Moon", "SCIENCE/The Sun", "SCIENCE/new_topic.html"
I don't understand what that means?
 
Old 01-13-2012, 02:38 AM   #4
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
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.

Code:
array_multisort(array_map('filemtime', $dirArray), SORT_ASC, $dirArray);
 
Old 01-13-2012, 02:52 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well, ask help from the support then, they will know what that means.
 
Old 01-13-2012, 07:39 AM   #6
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,

What I would try with your code is:
Code:
// open this directory 
$myDirectory = opendir("SCIENCE");

change it to :
// open this directory 
$myDirectory = opendir('./SCIENCE');
see the change of double quotes " to single ' and adding ./ path in front of the name

then you will probably get the array of filenames.

PHP readdir

good luck

Last edited by lithos; 01-13-2012 at 07:40 AM.
 
Old 01-13-2012, 12:03 PM   #7
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
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>&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"SCIENCE/$dirArray[$index]\">$dirArray[$index]</a>&nbsp;");
		print("is a ".filetype("SCIENCE/".$dirArray[$index]).",&nbsp;");
		print("its size is ".filesize("SCIENCE/".$dirArray[$index])." byte");
		if(filesize("SCIENCE/".$dirArray[$index]) != 1)
			print("s");
Code:
Works
		print("<br>&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"$dirArray[$index]\">$dirArray[$index]</a>&nbsp;");
		print("is a ".filetype($dirArray[$index]).",&nbsp;");
		print("its size is ".filesize($dirArray[$index])." byte");
		if(filesize($dirArray[$index]) != 1)
			print("s");
 
Old 01-13-2012, 12:08 PM   #8
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Where is SCIENCE dir ? Is it in same directory as index.php file ?

If yes, try:
PHP Code:
$myDirectory opendir("SCIENCE");

// get each entry
while($entryName readdir($myDirectory)) {
    
$dirArray[] = './SCIENCE/' $entryName;


Last edited by Cedrik; 01-13-2012 at 12:11 PM.
 
Old 01-13-2012, 12:09 PM   #9
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Ouch I was wrong It's that function being stubborn

---------- Post added 01-13-12 at 02:09 PM ----------

no its a directory in that files directory

---------- Post added 01-13-12 at 02:10 PM ----------

Oh yea its in the same directory so not no but yea
 
Old 01-13-2012, 12:18 PM   #10
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Is there any other way I can sort $dirArray in ascending order by the date directories where created that might be the problem.
 
Old 01-13-2012, 12:22 PM   #11
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Maybe work with full path ?

PHP Code:
$science_dir 'C:/Apache2.2/htdocs/forum/SCIENCE';

$myDirectory opendir($science_dir);

// get each entry
while($entryName readdir($myDirectory)) {
    
$dirArray[] = $science_dir '/' $entryName;

 
Old 01-13-2012, 12:23 PM   #12
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Still didn't work.

Quote:
Originally Posted by Cedrik View Post
Where is SCIENCE dir ? Is it in same directory as index.php file ?

If yes, try:
PHP Code:
$myDirectory opendir("SCIENCE");

// get each entry
while($entryName readdir($myDirectory)) {
    
$dirArray[] = './SCIENCE/' $entryName;

 
1 members found this post helpful.
Old 01-13-2012, 12:25 PM   #13
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
I didnt get any errors doing that though.
 
1 members found this post helpful.
Old 01-13-2012, 12:32 PM   #14
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Script may need little mod to work with fullpath
PHP Code:
for($index=0$index $indexCount$index++) 
{
    
$file basename($dirArray[$index]);

    if (
substr($file01) != "."
        
&& $file != "index.php"
        
&& $file != "new_topic.html")
    {
        print(
"<br>&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"$dirArray[$index]\">$file</a>&nbsp;");
        print(
"is a ".filetype($dirArray[$index]).",&nbsp;");
        print(
"its size is ".filesize($dirArray[$index])." byte");
        if(
filesize($dirArray[$index]) != 1)
            print(
"s");
    }

 
Old 01-13-2012, 12:38 PM   #15
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
It seems to work thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax highlighting for php in emacs bm1 Linux - Software 2 04-15-2017 04:29 AM
a php syntax question "==@" ?? snowtigger Programming 5 06-13-2008 10:22 AM
C++ syntax highlighting with PHP ckoniecny Programming 1 08-11-2006 07:09 AM
If Then PHP syntax helpme DropHit Programming 1 07-06-2004 11:27 PM
PHP Trouble Syntax Gerardoj Programming 2 03-30-2004 09:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration