Streaming Music Web-server pt6

Streaming Music Web-server pt6

So let’s continue on with our music server in small chunks 🙂 For today we are going to scan the directory and output the results to our browser.

This may seem like a very simple task but it is a very important one. Once our music server is up and running we will need to point to to a location and have it read the files so we know what we are dealing with.

So before we get started I want to share a helpful tip. DO NOT Reinvent the wheel! As a developer/programmer/coder research is your friend. Learn to look for examples online and use them. Depending on the language you are using read the documentation.

Now that we have that out of the way we will create a file called scan-directory.php and place it in our web directory path.

Here is the code in the file:

<?PHP

	/* Scan the directory and print out the files */

	//Directory to be scanned
	$directory = '/var/www/html/Techrens-Music-Server';

	//Results put into a array excluding ..
	$scanned_directory = array_diff(scandir($directory), array('..', '.'));

	//Output the array for debugging purposes
	//print_r($scanned_directory);

	//Loop through the array and output the name of the files
	foreach ($scanned_directory as $value) {
     echo $value . "<br />";
	}

?>

Next open your web browser and enter your server address + scan-directory.php and GO! Your output should be similar to this:

Pretty simple right? For homework how about trying to only display files that are mp3s.

If you have any questions please feel free to ask in the comment section. Also if your just plain lazy feel free to copy the entire source code from git 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *