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
1,440 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
  • 5
  • 4
  • 2
11 Comments
 
LVL 108

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 108

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Accepted Solution

by:
ray-solomon earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now