Except where otherwise noted, the contents of this document are Copyright 2010 Marty Stepp and Jessica Miller.
original lab idea and code by Brian Le; revised by Marty Stepp
Brian has written a page named music.html
to display his MP3s. He has already written the basic HTML and CSS code. But the HTML is redundant, and he must manually edit it every time he adds songs to his collection.
In this lab, you will turn this page into a dynamic PHP page in a file named music.php
that removes redundancy and dynamically lists the MP3s on the computer. Download the HTML file below to get started:
(See example screenshot on next slide.)
Let's turn music.html
into a PHP page and make sure you can see PHP output on Webster.
music.html
and rename it to music.php
.
music.php
on Webster.
print
statement to the page as PHP (not HTML!) code.
The start of your page should look like this when you are done:
(See example screenshot on next slide.)
for
loop.
See book Chapter 5 or the lecture slides/examples if you can't remember PHP syntax.
The start of your page should look like this when you are done:
(See example screenshot on next slide.)
Currently the page hard-codes the list of MP3 file names. Let's change it to read the file names dynamically from the server's hard drive.
songs/
folder to Webster.
.mp3
files in the songs/
folder using the glob
function. (Pass a folder/file path to glob
, NOT a URL. For debugging, you may want to print_r
the array.)
li
list item. (Don't worry about any links or images or formatting; just print each as a bullet in the list.)
Your MP3 list should look like this when you are done:
(See example screenshot on next slide.)
Now let's format the MP3 links to look like the original hard-coded ones.
li
a class
of mp3item
.
songs/
folder.
basename
function to extract the file name from a larger path string. e.g. basename("a/b/c.txt")
returns "c.txt"
.
a
tag whose href
target is the same as the song file's path.)
Your MP3 list should look like this when you are done:
(See example screenshot on next slide.)
Add code to display the contents of .m3u
playlists. A playlist is just a text file containing a list of song file names, one per line. It can also have comment lines that begin with #
. Example:
#EXTM3U #EXTINF:177,Paul Dateh & Oren Yoel - Be More be-more.mp3 #EXTINF:291,Christopher Toy - Just Because just-because.mp3
.m3u
files in the songs/
directory, and for each one, displays a list of the songs in that playlist.
Use the strpos
function to search a line for a substring.
Your page should look like this when you are done (scroll to the bottom):
(See example screenshot on next slide.)
Display each MP3's size in appropriate units. Use the filesize
function.
If displaying kb or mb, show the value as a whole number.
The songs should look like this on the page when you are done:
drift-away.mp3
) would go first and smallest songs (such as cse-190M-rap.mp3
) last.
If you finish all the exercises, you can add any other content or code you like to your page.
If the lab is over or almost over, check with a TA and you may be able to be dismissed.
Great work!