PHP code : how to scan a web-folder for mp3 files and generate a sorted numbered file-list?

Hi

Note : this process/files will run on web server means on internet not on local computer.

I want to code php file (php code) which will capable of scanning a web-folder for mp3 files and then generate a alphabatically sorted numbered list of files with file-size as well. It should also check for duplicate files and ignore duplicate.

I am using following code to do just listing (no sorting) but after 2-3 records it prints false record which is not present in web-folder and then continues means work properly. I am unable to know why it prints false record.

Here is my code :

========================PHP Code===========================

<?PHP

// Define the full path to your folder from root
$path = "audio";

// Open the folder
$dir_handle = @opendir($path) or die("Cannot open the damn file $path");
$numb=1;

// Loop through the files
while ($file = readdir($dir_handle)) {

if($file == ".mp3" )

continue;

$fichierexp = $path."/".$file.".txt";
$TheLinkedFile = $path."/".$file;
if(file_exists($fichierexp)) {
@$fp = fopen($fichierexp,'r');
echo "<a href=\"$TheLinkedFile\" target=\"blank\">".fgets($fp,999)."</a><br>";
fclose($fp);
} else {

echo "<b>$numb.   ". "         $file</b><br>"."{play}audio/$TheLinkedFile{/play}";
$filename = $TheLinkedFile;
$bawa1=filesize($filename);
$bawa= round(($bawa1)/1024/1024, 2);
echo 'File Size :'. ($bawa) . ' MB<br><br>';
$numb=$numb+1;
}
}
// Close
closedir($dir_handle);
?>
========================PHP Code===========================

Well look at http://thegurbani.com/index.php/Static-Category/audio.html this page

here ( 3. .. ) is false record
and (7. .) is false record.

Plz correct this error.

theredcodeAsked:
Who is Participating?
 
ray-solomonConnect With a Mentor Commented:
sure, no problem.
<?php
	$list = 'list.txt';
	
	if (file_exists($list))
	{
		$directories = file($list, FILE_IGNORE_NEW_LINES);
	
		if (count($directories) >0)
		{
			foreach ($directories as $directory)
			{
				if (is_dir($directory))
				{
					$contents = array();
					$readdir = @opendir($directory);
					while (false !== ($files = @readdir($readdir)))
					{
						if ($files != '.' && $files != '..' && strpos($files, '.mp3'))
						{
							$contents[] = $files;
						}
					}
					@closedir($readdir);
					// remove duplicates
					$results = array_unique($contents);
					$n = 1;
					echo '<br><br><strong>'.$directory.'</strong><br>';
					foreach ($results as $file)
					{
						echo $n.': <a href="'.$directory.$file.'" target="blank">'.$file.'</a><br>';
						echo 'File Size :'.round(filesize($directory.$file)/1024/1024, 2).'<br>';
						$n++;
					}
				}
				else
				{
					echo 'Cannot open the directory: '.$directory;
				}
			}
		}
		else
		{
			echo 'There are no directories in the file '.$list;
		}
	}
	else
	{
		echo 'This file does not exist: '.$list;
	}
?>

Open in new window

0
 
Ray PaseurCommented:
PHP4 or PHP5?
0
 
theredcodeAuthor Commented:
I have both on my server
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Ray PaseurCommented:
Try something like this
<?php
/* ************************************************************************* */
function showFileSize ($file_link) {
 
	$xb = filesize(trim($file_link));
	$tb = 1024*1024*1024*1024;
	$gb = 1024*1024*1024;
	$mb = 1024*1024;
	$kb = 1024;
	   if ($xb >= $tb)	{ $text = number_format(($xb / $tb),3) . " TB"; }
	elseif ($xb >= $gb) { $text = number_format(($xb / $gb),2) . " GB"; }
	elseif ($xb >= $mb) { $text = number_format(($xb / $mb),1) . " MB"; }
	elseif ($xb >= $kb) { $text = number_format(($xb / $kb),0) . " KB"; }
	elseif ($xb >= 0)   { $text = number_format( $xb       ,0) . " bytes"; }
	else                { $text = "0 bytes"; }
 
return ( $text ); }
/* ************************************************************************** */
 
 
$dir	= getcwd();
unset($file_array);
 
// Open my directory, and proceed to gather its contents
if ($dh = opendir($dir)) { // echo "<br />OPENDIR $my_file_path\n";
	while (($filename = readdir($dh)) !== false) {
		if (!eregi('(\.mp3$)',$filename)) continue;
		$file_array[]  = $dir .'/'. $filename;
	}
	closedir($dh);
	if (!is_array($file_array)) { die("No MP3 in $dir"); }
	sort ($file_array);
	reset($file_array);
} // END OPENDIR
 
 
if (!is_array($file_array)) { die("No MP3 in $dir"); }
 
$file_array = array_values($file_array);
natcasesort($file_array);
 
$kount = count($file_array);
 
while ($kount > 0) {
	$kount--;
	$fname = $file_array[$kount];
 
	$t_size	= showFileSize($fname);
	$t_time	= date ('Y-m-d H:i:s',filemtime($fname));
	echo "<br /><a href=\"$fname\">$fname</a> $t_size $t_time \n";
}

Open in new window

0
 
ray-solomonCommented:
Tested. this should work for you.
<?php
 
	$directory = 'audio/';
	$contents = array();
	
	if (is_dir($directory))
	{
		$readdir = @opendir($directory);
		while (false !== ($files = @readdir($readdir)))
		{
			if ($files != '.' && $files != '..' && strpos($files, '.mp3'))
			{
				$contents[] = $files;
			}
		}
		@closedir($readdir);
		// remove duplicates
		$results = array_unique($contents);
		$n = 1;
		foreach ($results as $file)
		{
			echo $n.': <a href="'.$directory.$file.'" target="blank">'.$file.'</a><br>';
			echo 'File Size :'.round(filesize($directory.$file)/1024/1024, 2).'<br>';
			$n++;
		}
	}
	else
	{
		echo "Cannot open the damn file $directory";
	}
?>

Open in new window

0
 
theredcodeAuthor Commented:
Thanks. You are genius.. words are not just enought to thanks you and reward your skills.

Is it possible for this to scan a text file for "directories i.e. webfolders" so that we  can include  more than one directory.

The text file will contain one directory/folder path per line
for example:

========list.txt===========
audio/
video/
aaa/bbb/ccc/flash
123/654/000/okay/more/files
=========list.txt==========

Then output should be
<bold>Directory : audio <bold>
List of files in it
.
..
...

Is it possible for this to scan a text file for "directories i.e. webfolders" so that we  can include  more than one directory.

The text file will contain one directory/folder path per line
for example:

========list.txt===========
audio/
video/
aaa/bbb/ccc/flash
123/654/000/okay/more/files
=========list.txt==========

Then output should be
<bold>Directory : audio <bold>
List of files in it
.
..
...

Is it possible for this to scan a text file for "directories i.e. webfolders" so that we  can include  more than one directory.

The text file will contain one directory/folder path per line
for example:

========list.txt===========
audio/
video/
aaa/bbb/ccc/flash
123/654/000/okay/more/files
=========list.txt==========

Then output should be
<bold>Directory : audio <bold>
List of files in it
.
..
...

Is it possible for this to scan a text file for "directories i.e. webfolders" so that we  can include  more than one directory.

The text file will contain one directory/folder path per line
for example:

========list.txt===========
audio/
video/
aaa/bbb/ccc/flash
123/654/000/okay/more/files
=========list.txt==========

Then output should be
<bold>Directory_name<bold>
List of files in it
.
..
...

<bold>Directory_name<bold>
List of files in it
.
..
...

<bold>Directory_name<bold>
List of files in it
.
..
...

<bold>Directory_name<bold>
List of files in it
.
..
...

<bold>Directory_name<bold>
List of files in it
.
..
...
0
 
theredcodeAuthor Commented:
Try : http://thegurbani.com/scan.php

with above code, i am getting file size error for each scanned file. on line 31...

i.e. Warning: filesize() [function.filesize]: stat failed for audio2.mp3 in /hsphere/local/home/savesikhi/thegurbani.com/scan.php on line 31

have a look plz thanks.
0
 
ray-solomonCommented:
How is your directory structure setup?

it should be similar to this: (/ is the root of your site)


/audio
/audio/moreMP3
/moremusic/


Then inside the list.txt, it would look like this:
audio/
audio/moreMP3/
moremusic/

The list.txt can be in any directory you want as long as it is linked up properly in the script.
$list = 'list.txt';
or
$list = 'audio/list.txt';
..etc


0
 
theredcodeAuthor Commented:
Thanks, great job done.
0
 
theredcodeAuthor Commented:
Any how, i modified your code to this... (list.txt will remain the same no slash "/" at end)

<?php
        $list = 'scan.txt';
       
        if (file_exists($list))
        {
                $directories = file($list, FILE_IGNORE_NEW_LINES);
       
                if (count($directories) >0)
                {
                        foreach ($directories as $directory)
                        {
                                if (is_dir($directory))
                                {
                                        $contents = array();
                                        $readdir = @opendir($directory);
                                        while (false !== ($files = @readdir($readdir)))
                                        {
                                                if ($files != '.' && $files != '..' && strpos($files, '.'))
                                                {
                                                        $contents[] = $files;
                                                }
                                        }
                                        @closedir($readdir);
                                        // remove duplicates
                                        $results = array_unique($contents);
                                        $n = 1;
                                        echo '<br><br><strong>'.$directory.'</strong><br>';
                                        foreach ($results as $file)
                                        {
                                                echo $n.': <a href="'.$directory."/".$file.'" target="blank">'.$file.'</a><br>';

                                                echo 'File Size : '.round(filesize($directory."/".$file)/1024/1024, 2).'MB<br>';
                                                                      $n++;
                                        }
                                }
                                else
                                {
                                        echo 'Cannot open the directory: '.$directory;
                                }
                        }
                }
                else
                {
                        echo 'There are no directories in the file '.$list;
                }
        }
        else
        {
                echo 'This file does not exist: '.$list;
        }
?>


Thanks very much, you are so helpful.
0
 
ray-solomonCommented:
i see, glad I could be some help
:-)
0
All Courses

From novice to tech pro — start learning today.