Hey all,
I would really appreciate it if you can help me with this problem.
The basic idea, read all the files in a folder, and print them as links to each file.
Which does work.
But now, after it gets out of the foreach loop, I need it to keep the array of $fileNames.
Because I need to reuse it after, but it doesn't. And I don't get why.

Thanks in advance, code is following.

$siteName = 'my site';
$fileNames = array();
$fileNumber = 0;
$directory = 'applications/bussSites';
$dir = new DirectoryIterator($directory);
foreach($dir as $file ){
	if(($file != ".") && ($file != "..")){
		$fileNames[$fileNumber] = $file;
		echo '<a href="'.$siteName.$directory.'/'.$file.'">'.$file.'<a><br />';
		$fileNumber++;
	}
	
	echo "=".$fileNumber."--".$fileNames[$fileNumber-1]."<br/>";
}
//echo $fileNames[3]."<br/>";
echo "========================\n";
for($i=0; $i<$fileNumber; $i++){
	echo $fileNames[$i+1]."<br />";
}
echo "<br />";
echo "<a>Number: ".$fileNumber."</a>";
echo '</div>';

and I get this:

1_aple.txt
=1--1_aple.txt
3_jasd.txt
=2--3_jasd.txt
10_jad.txt
=3--10_jad.txt
7_pdjf.txt
=4--7_pdjf.txt
=4--.
=4--..
2_jdha.txt
=5--2_jdha.txt
======================== 





Number: 5

Recommended Answers

All 6 Replies

Any ideas? I would love to try them.
I really need help in this...

When you loop over the DirectoryIterator object it is returning DirectoryIterator objects, not strings containing filenames.

At the end of your script add a line like this:

echo "<pre>".print_r($fileNames,true)."</pre>";

print_r will show you what $fileNames contains. You can use the getFilename() method to get the filename and store it into the array instead of the objects.

Your not losing any variables it is just that . and .. are directories not files.

Thanks for the reply madcoder,

I'm new to these things, would you be able to explain to me what "$fileNames" actually contains in my code? Like what is "DirectoryIterator object" exactly?

Secondly, the way I would use "getFilename()" is like this "getFilename($fileNames[$fileNumber])" based on my code? or am I understanding you wrong?

Thanks in advance.

rch1231, thanks for the attempt but that was not my question, I realize these are directories. but if you look after "==...==" it does not "reprint" the file names as I requested it in the php code. My question, WHY NOT? where did the file names go?

$fileNames in your code contains an array of DirectoryIterator class objects. The DirectoryIterator class is documented at http://php.net/manual/en/class.directoryiterator.php

In case you are really new to php or programming in general, an object is a collection of both data/variables (properties) and functions (methods) that represent something. A class is the definition in code and an object is a created instance of a class. The new operator is (usually) used to create an instance of a class. To access the (public) properties and methods of an object you use the '->' operator (dash, greater than.)

<?php
$currentPath = dirname(__FILE__);
//  This says create a new DirectoryIterator object and call it $uploadsDir.
$uploadsDir = new DirectoryIterator($currentPath);  
$onlyNames = print_contents($uploadsDir);

function print_contents($directory)
{
	$filenames = array();
?>
<table>
<tr><th>Name</th><th>Type</th><th>Changed on</th></tr>
<?php
	# DirectoryIterator is a class that can be looped over like an 
	# array (hinted at by the word Iterator in the name.
	foreach($directory as $dirEntry)
	{
		// Each $dirEntry is another DirectoryIterator object.  It can represent a directory, file, or link (symbolic or hard link.)
		if($dirEntry->isDot())  // Method that checks for . or ..
		{
			continue;
		}
	
		printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n", $dirEntry->getFilename(), $dirEntry->getType(), date('n/j/Y',$dirEntry->getCTime()));
		array_push($filenames, $dirEntry->getFilename);
	}
?>
</table>
<?php	
	return $filenames;
}
?>

-------

Secondly, the way I would use "getFilename()" is like this "getFilename($fileNames[$fileNumber])" based on my code? or am I understanding you wrong?

No, but it is legal to do this:

echo $filenames[$fileNumber]->getFilename();

I really do appreciate your response and thanks.

I was a little hasten to post my reply, because just a few minutes after I looked into your first response, I found those pages and how to get the fileName.

I'm actually not new to programing, I have done a couple of languages before, just new to php, and some stuff it in, I didn't realize by object you meant a class, when I found that page, I realized that. ;)

Also, I was just about to post a reply that I found it.

Anyways, Thank you very much ;) Problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.