Dir Script mag alleen mappen, mp3 en wav bestanden tonen.
Ik zit me al sinds gister rot te zoeken naar een script wat een dir uitleest en aan mij wensen voldoet.
Nou heb ik eindelijk een script dat ook mappen weergeeft. Alleen het voldoet nog niet helemaal aan mijn eisen.
- Mappen moeten boven de bestanden staan
- Alle extensies behalve mp3 en wav mogen niet weergegeven worden
- Geen extensie achter de bestandsnaam tonen
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
$allowed = array(
'gast' => 'music4free', //gebruikers zonder hoofdletters, wachtwoorden mogen alle tekens bevatten
'admin' => 'frans13',
);
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($allowed[strtolower($_SERVER['PHP_AUTH_USER'])]) || $allowed[strtolower($_SERVER['PHP_AUTH_USER'])] != $_SERVER['PHP_AUTH_PW'])
{
header('WWW-Authenticate: Basic realm="Administratie Paneel"');
header('HTTP/1.0 401 Unauthorized');
echo 'U heeft geen rechten om hier te zijn';
exit;
}
?>
<title>Muziek overzicht</title>
<?php
$music = "../audio.gif";
$map = "../dir.gif";
?>
<?php
error_reporting(E_ALL);
function GetExt($filename) {
$atemp = explode(".", $filename);
return strtolower($atemp[sizeof($atemp) - 1]);
}
function fsize($file) {
$file = substr($file, 1);
$mult = "kB";
$prec = 0;
$size = filesize($file);
if(round($size/1024,0)) {
$size /= 1024;
$mult = "kB";
$prec = 3;
}
if(round($size/1024,0)) {
$size /= 1024;
$mult = "mB";
$prec = 3;
}
return(round($size,$prec)." ".$mult);
}
?>
<table cellspacing="0" cellpadding="1" width="64%">
<tr>
<td></td>
<td width="62%"><b> Naam:</b></td>
<td width="15%"><b>Mp3</b></td>
<td width="11%"><b>Map</b></td>
<td width="11%"><b>Mp3</b></td>
</tr>
<?php
$files = array();
$handle=opendir("./");
while (false!==($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files[] = $file;
}
}
sort($files);
foreach($files AS $file){
$ext = GetExt($file);
?>
<tr>
<td width=5% align="center">
<?php
if ($ext == "mp3" OR $ext == "wav"){
echo "<img src=$music>";
}
else{
echo "<img src=$map>";
}
?>
</td>
<td width="62%"><?php echo $file; ?></td>
<td width=15%><a href=".<?php echo "/".$file; ?>">Downloaden</a></td>
<td width="11%"><a href=".<?php echo "/".$file; ?>/">Openen</a></td>
<td width="15%"><a href=".<?php echo "/".$file; ?>">Afspelen</a></td>
</tr>
<?php
}
closedir($handle);
?>
</table>
$allowed = array(
'gast' => 'music4free', //gebruikers zonder hoofdletters, wachtwoorden mogen alle tekens bevatten
'admin' => 'frans13',
);
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($allowed[strtolower($_SERVER['PHP_AUTH_USER'])]) || $allowed[strtolower($_SERVER['PHP_AUTH_USER'])] != $_SERVER['PHP_AUTH_PW'])
{
header('WWW-Authenticate: Basic realm="Administratie Paneel"');
header('HTTP/1.0 401 Unauthorized');
echo 'U heeft geen rechten om hier te zijn';
exit;
}
?>
<title>Muziek overzicht</title>
<?php
$music = "../audio.gif";
$map = "../dir.gif";
?>
<?php
error_reporting(E_ALL);
function GetExt($filename) {
$atemp = explode(".", $filename);
return strtolower($atemp[sizeof($atemp) - 1]);
}
function fsize($file) {
$file = substr($file, 1);
$mult = "kB";
$prec = 0;
$size = filesize($file);
if(round($size/1024,0)) {
$size /= 1024;
$mult = "kB";
$prec = 3;
}
if(round($size/1024,0)) {
$size /= 1024;
$mult = "mB";
$prec = 3;
}
return(round($size,$prec)." ".$mult);
}
?>
<table cellspacing="0" cellpadding="1" width="64%">
<tr>
<td></td>
<td width="62%"><b> Naam:</b></td>
<td width="15%"><b>Mp3</b></td>
<td width="11%"><b>Map</b></td>
<td width="11%"><b>Mp3</b></td>
</tr>
<?php
$files = array();
$handle=opendir("./");
while (false!==($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files[] = $file;
}
}
sort($files);
foreach($files AS $file){
$ext = GetExt($file);
?>
<tr>
<td width=5% align="center">
<?php
if ($ext == "mp3" OR $ext == "wav"){
echo "<img src=$music>";
}
else{
echo "<img src=$map>";
}
?>
</td>
<td width="62%"><?php echo $file; ?></td>
<td width=15%><a href=".<?php echo "/".$file; ?>">Downloaden</a></td>
<td width="11%"><a href=".<?php echo "/".$file; ?>/">Openen</a></td>
<td width="15%"><a href=".<?php echo "/".$file; ?>">Afspelen</a></td>
</tr>
<?php
}
closedir($handle);
?>
</table>
Gewijzigd op 01/01/1970 01:00:00 door Patrick Vaarkamp
Je zou dit eens kunnen proberen:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?PHP
define('NLBR', "<br />\n");
$dir = new DirectoryIterator('.'); // http://nl3.php.net/manual/en/class.directoryiterator.php
$disallowed_files = array('Index.php'); // Bestanden die niet mogen worden weergegeven
foreach($dir as $file)
{
if(!$file->isDot())
{
if(is_dir($file))
{
$files[] = urlencode($file). '/';
}
elseif(is_file($file))
{
$files[] = urlencode($file);
}
if(!in_array($file, $disallowed_files))
{
if ( is_dir($file) )
{
$dirs[] = '(DIR) ' . $file;
}
if ( is_file($file) )
{
$ext = substr($file, -3);
if( ($ext != 'mp3') && ($ext != 'wav') )
{
$bestanden[] = '(' . $ext . ') ' . $file;
}
}
}
}
}
sort($dirs);
foreach ($dirs as $directory)
{
echo $directory . NLBR;
}
sort($bestanden);
foreach ($bestanden as $bestand)
{
echo $bestand . NLBR;
}
?>
define('NLBR', "<br />\n");
$dir = new DirectoryIterator('.'); // http://nl3.php.net/manual/en/class.directoryiterator.php
$disallowed_files = array('Index.php'); // Bestanden die niet mogen worden weergegeven
foreach($dir as $file)
{
if(!$file->isDot())
{
if(is_dir($file))
{
$files[] = urlencode($file). '/';
}
elseif(is_file($file))
{
$files[] = urlencode($file);
}
if(!in_array($file, $disallowed_files))
{
if ( is_dir($file) )
{
$dirs[] = '(DIR) ' . $file;
}
if ( is_file($file) )
{
$ext = substr($file, -3);
if( ($ext != 'mp3') && ($ext != 'wav') )
{
$bestanden[] = '(' . $ext . ') ' . $file;
}
}
}
}
}
sort($dirs);
foreach ($dirs as $directory)
{
echo $directory . NLBR;
}
sort($bestanden);
foreach ($bestanden as $bestand)
{
echo $bestand . NLBR;
}
?>
Met vriendelijke groet,
Jeroen
de handleiding):
Of wat netter (zie ook Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
//Classes
class DirectoryFilter extends FilterIterator {
/**
* Constructor
*
* @param string $path
*
*/
public function __construct($path) {
parent::__construct(new DirectoryIterator($path));
}
/**
* Conditions to determine acceptation
*/
public function accept() {
return !$this->getInnerIterator()->isDot() &&
$this->getInnerIterator()->isDir();
}
}
class FileFilter extends FilterIterator {
/**
* @var array $_allowed (extensions allowed)
*/
private $_allowed = array('mp3', 'wav');
/**
* Constructor
*
* @param string $path
*
*/
public function __construct($path) {
parent::__construct(new DirectoryIterator($path));
}
/**
* Conditions to determine acceptation
*/
public function accept() {
$extension = pathinfo($this->getInnerIterator()->getFilename(), PATHINFO_EXTENSION);
return !$this->getInnerIterator()->isDot() &&
$this->getInnerIterator()->isFile() &&
in_array($extension, $this->_allowed);
}
}
//Directories
$path = new DirectoryFilter('./');
echo '<strong>Directories:</strong><br />';
foreach ($path as $dir) {
echo $dir . '<br />';
//process each $dir
}
//Files
$path = new FileFilter('./');
echo '<strong>Files:</strong><br />';
foreach($path as $file) {
echo $file . '<br />';
//process each $file
}
?>
//Classes
class DirectoryFilter extends FilterIterator {
/**
* Constructor
*
* @param string $path
*
*/
public function __construct($path) {
parent::__construct(new DirectoryIterator($path));
}
/**
* Conditions to determine acceptation
*/
public function accept() {
return !$this->getInnerIterator()->isDot() &&
$this->getInnerIterator()->isDir();
}
}
class FileFilter extends FilterIterator {
/**
* @var array $_allowed (extensions allowed)
*/
private $_allowed = array('mp3', 'wav');
/**
* Constructor
*
* @param string $path
*
*/
public function __construct($path) {
parent::__construct(new DirectoryIterator($path));
}
/**
* Conditions to determine acceptation
*/
public function accept() {
$extension = pathinfo($this->getInnerIterator()->getFilename(), PATHINFO_EXTENSION);
return !$this->getInnerIterator()->isDot() &&
$this->getInnerIterator()->isFile() &&
in_array($extension, $this->_allowed);
}
}
//Directories
$path = new DirectoryFilter('./');
echo '<strong>Directories:</strong><br />';
foreach ($path as $dir) {
echo $dir . '<br />';
//process each $dir
}
//Files
$path = new FileFilter('./');
echo '<strong>Files:</strong><br />';
foreach($path as $file) {
echo $file . '<br />';
//process each $file
}
?>
Gewijzigd op 01/01/1970 01:00:00 door Mark PHP
Dank je Agirre, zal die handleiding is doorlezen!