Ecco lo script completo.
Il file scansione.php:
Codice PHP:
<?
function getFiles($directory) {
include ("connessione_db.php.inc");
if($dir = opendir($directory)) {
// Create an array for all files found
$tmp = Array();
// Add the files
while($file = readdir($dir)) {
// Make sure the file exists
if($file != "." && $file != ".." && $file[0] != '.') {
// If it's a directiry, list all files within it
//echo ("$file");
if(is_dir($directory . "/" . $file)) {
$tmp2 = getFiles($directory . "/" . $file);
if(is_array($tmp2)) {
$tmp = array_merge($tmp, $tmp2);
}
} else {
array_push($tmp, $directory . "/" . $file);
$f = $file;
$e = str_replace("./brani/", "", $directory);
//echo ("$e - $f
");
include ("estrai_dati.php");
$autore = addslashes(htmlentities($info['artist'], ENT_QUOTES));
if ($info['date'] != "") { $anno_ver = $info['date'];}
else { $anno_ver = "";}
while (list($key) = each($info)) {
if (preg_match('/.*title.*/i', $key, $matches)) {
$title = $info[$matches[0]];
unset($info[$key]);
$info['title'] = $title;
break;
}
}
$dimensione = $grandezza_mega;
$cartella = $e;
$album = $e;
$nomefile = $f;
$controlla_file = mysql_query("SELECT * FROM mp38 where nomefile = '$f'
AND cartella = '$e';");
if (mysql_num_rows($controlla_file) < 1 AND $e != '.' AND $f != '..' ) {
$add = mysql_query("INSERT INTO mp38 VALUES ('' ,
'$info[title]' , '$autore' , '' , '$anno_or' , '$anno_ver' , 'no' ,
'$nomefile' , '$cartella' , '$album', '$dimensione' , '' , 'no' ,
'$note');");
// stampa gli eventuali errori
//echo mysql_errno() . ": " . mysql_error(). "\n";
//echo ("<div style=\"font-size:10px; font-family:
Verdana;\">$title<div>");
unset($info['title']);
}
}
}
}
// Finish off the function
closedir($dir);
return $tmp;
}
mysql_close ($connect_db);
}
// Example of use
getFiles('./brani'); // This will find all files in the current
directory and all subdirectories
?>
e il file estrai_dati.php che viene incluso in scansione.php:
Codice PHP:
<?
$filename="./brani/$e/$f";
$fp = fopen( $filename, 'r');
$info = array();
$info['size'] = filesize( $filename );
$info['name'] = basename( $filename );
$info['path'] = dirname( $filename );
$done = false;
while( $done == false ) {
$working = fread( $fp, 1 );
if( $working == "l" ) {
$working .= fread( $fp, 8 );
//print $working.'
';
if( $working == "libVorbis" ) {
$done = true;
//echo ("sergio");
}
}
}
while( ord( $working ) > 31 ) {
$working = fread( $fp, 1 );
}
$tag = "";
while( $tag != "done" ) {
$working = "";
while( ( $working != "=" ) && ( $tag != "done" ) ) {
if( ord( $working ) > 31 ) {
$tag .= $working;
}
$working = fread( $fp, 1 );
if( $tag == "v" ) {
$tag .= "o".fread( $fp, 9 );
if( ( substr_count( $tag, "vorbis" ) == 1 ) && (
substr_count( $tag, "BCV" ) == 1 ) ) {
$tag = "done";
} else {
fseek( $fp, ( ftell( $fp ) - 10 ) );
}
}
}
if( $tag != "done" ) {
$working = fread( $fp, 1 );
while( ord( $working ) > 31 ) {
$title .= $working;
$working = fread( $fp, 1 );
}
$info[$tag] = $title;
$tag = "";
$title = "";
}
}
fclose( $fp );
// calcolo la grandezza del file in megabyte
$grandezza_byte = $info['size'] / 1048576;
$grandezza_mega = round($grandezza_byte, 2);
//print_r($info);
return $info;
?>
Sergej