Jump to content

Get files from directory


phpretard

Recommended Posts

I almost have it but can't figure why the code below only shows one file per folder.

 

I think it is showing the last file.

 

I need $ul somehow looping to show all the files in $file.

 

<?php
$year = date('Y');

if ($handle = opendir("results/$year/Boys")) {

while (false !== ($file = readdir($handle))) {

	if ($file != "." && $file != "..") {

	if ($handle2 = opendir("results/$year/Boys/$file")) {

		while (false !== ($file2 = readdir($handle2))) {

			if ($file2 != "." && $file2 != "..") {
				$ul = "<li><a href=\"#\">$file2</a></li>";

				$li =  "<li><a target=\"_blank\" href=\"javascript:void(0)\">$file</a>	
							<ul>
							$ul
							</ul>
						</li>";

				} // close if file2		
			}echo $li;
		}
	}
}
}closedir($handle);closedir($handle2);
?>

Link to comment
Share on other sites

Out of curiosity why do you have 2 handles??

 

I am working on a file system class for a project here at work, this will get all the files from the directory passed:

 

public function loadFiles($directory, $wantedExts)
{
	/**
	 * @var array $fileList a list of files in the directory
	 */
	$fileList = array();
	$extension = explode(",", $wantedExts);
	echo count($extension);
	print_r ($extension);

	/*
	 * Check to see if the current file in the directory is of the correct type.
	 * 
	 * This method excludes . and .. listings to make sure they are not recursive and
	 * files contained wihtin the directory.
	 * 
	 * If it is what we want, push it on the $fileList array.
	 * 
	 * @TODO:
	 * 			* Accept multiple file names, like "csv,xls"
	 */

	// Check to see if the directory is valid and accessible
	if ($handle = opendir($directory)) {


		// Read the files in the directory
		while (false !== ($file = readdir($handle))) {
			/*
			 * Not a directory or other level of the tree
			 */
			/*
			if ($file != "." && $file != ".." && $this->checkExt($file, $extention)) {
				array_push($fileList, $file);
			}
			*/
			if ($file != "." && $file != "..")
			{
				/*
				if (!empty($extention) 
					&& $this->checkExt($file, $extension))
				{
				*/echo $this->getExt($file);
				if (isset($extension) 
					&& (count($extension) > 0)
					&& in_array($this->getExt($file), $extension, FALSE))
				{
					array_push($fileList, $file);
				} else {
					/*
					 * Get all files
					 */
					array_push($fileList, $file);
				}
			}
		}
	} else {
		die ("<div class='error'>Invalid directory!</div>");
	}
	unset($extension);
	return $fileList;

}

Link to comment
Share on other sites

You might need this

 

/**
 * public function getExt($file)
 * 
 * Returns a file's extention.
 * 
 * @param string file to parse extention
 * @return string file's extention
 */
public function getExt($file)
{
	return pathinfo($file, PATHINFO_EXTENSION);
}

Link to comment
Share on other sites

I am using 2 because I first need to get the set of folders to build the first menu then go into the folder and display the files as the sub menu.

 

Does the code below clarify the question?

 


<?php

if ($handle = opendir("results/$year/Girls")) {

while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
		echo "<li><a target=\"_blank\" href=\"javascript:void(0)\">$file</a>	
				<ul>
				<li><a href=\"#\">NEED FILE 1 FROM FOLDER $file</a></li>
				<li><a href=\"#\">NEED FILE 2 FROM FOLDER $file</a></li>
				<li><a href=\"#\">NEED FILE 3 FROM FOLDER $file</a></li>
				<li><a href=\"#\">NEED FILE 4 FROM FOLDER $file</a></li>
				</ul>
			</li>";
        }
    }closedir($handle);
}

?>

Link to comment
Share on other sites

oh ok, i was quickly skimming and over looked the dir handle

 

anyway it looks good to me too. almost what I have.

 

Try commenting out the echo with the list in it and just print $file.  what comes out.

 

Stupid check, how many files are in the /results/2011/Girls dir

Link to comment
Share on other sites

<?php
$year = date('Y');

if ($handle = opendir("results/$year/Boys")) {

while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
		echo "<li><a href=\"javascript:void(0)\">$file</a>
		<ul>
		";

		if ($handle2 = opendir("results/$year/Boys/$file")) {
			while (false !== ($file2 = readdir($handle2))) {
				if ($file2 != "." && $file2 != "..") {
					echo "<li><a href=\"javascript:void(0)\">$file2</a>";
				}
			}
		}

		echo "</ul></li>";
        
	}
    }closedir($handle);closedir($handle2);
}

?>

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.