Initially I sent him off a simple script that would create a new text file for each mp3 file that he had in the directory. After sleeping a few hours I came to the conclusion that the solution sucked
This is my new solution using a comma separated value (CSV) flat file database.
- Code: Select all
<?php
//Initilized Vars
$file_types = array( 'mp3' , 'ogg' ); //accepted extensions
$dir = "music"; // directory
$count_csv = "counter.csv"; // flat-file database (comma separated values)
$file_count = array(); // the output
//var with the major stuff in it
$listing = array_output(csv_extract(), dir_read($file_types));
//Functions
function csv_extract($csv = "counter.csv")
{
$return = array();
if(!is_file($csv)){
touch($csv);
}
$fh = fopen($csv, "r");
while (!feof($fh)) {
$output = explode(",", fgets($fh, 4096));
if($output[0] !== "" && $output[1] !== ""){
$return[$output[0]] = trim($output[1]);
}
}
fclose($fh);
return $return;
}
function csv_inject($file, $input = array(), $csv = "counter.csv")
{
$output = "";
$cr = "\n";
$input[$file]++;
foreach($input as $key => $val){
if($key !== "" && $val !== ""){
$output .= trim($key) . "," . trim($val) . $cr;
}
}
$output = preg_replace("#(.*)\n$#", "\$1", $output);
$fh = fopen($csv, "w");
fwrite($fh, $output);
fclose($fh);
}
function dir_read($types = array('mp3'), $dir = "music")
{
$return = array();
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$ext = preg_replace("#(.*)\.([a-z0-9]{3})$#", "\$2", strtolower($file));
if(array_search( $ext, $types) !== FALSE){
$return[$file] = 0;
}
}
closedir($dh);
}
}
return $return;
}
function array_output($from_csv = array(), $return = array())
{
foreach($return as $key => $val){
$return[$key] = $from_csv[$key];
}
return $return;
}
//Page output
if(isset($_GET['download'])){
if(1===1){
echo '<meta http-equiv="Refresh" content="1;url='.$dir."/".$_GET['download'].'">';
csv_inject($_GET['download'], $listing);
}
}else{
foreach($listing as $key => $val){
echo "<a target='_blank' href='?download=".$key."'>".$key."</a> | Downloaded ".$val." times<br />";
}
}
?>
Functions explanations will be covered in the reply posts below






