Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-14
11
Medium Priority
?
1,524 Views
Last Modified: 2013-11-05
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.

0
Comment
Question by:theredcode
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 21786817
PHP4 or PHP5?
0
 

Author Comment

by:theredcode
ID: 21786836
I have both on my server
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 21786851
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:ray-solomon
ID: 21786873
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
 

Author Comment

by:theredcode
ID: 21786908
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
 
LVL 10

Accepted Solution

by:
ray-solomon earned 2000 total points
ID: 21786944
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
 

Author Comment

by:theredcode
ID: 21786985
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
 
LVL 10

Expert Comment

by:ray-solomon
ID: 21787022
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
 

Author Closing Comment

by:theredcode
ID: 31467263
Thanks, great job done.
0
 

Author Comment

by:theredcode
ID: 21787029
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
 
LVL 10

Expert Comment

by:ray-solomon
ID: 21787039
i see, glad I could be some help
:-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question