从其他驱动器获取mp3文件的问题

时间:2017-07-01 11:45:30

标签: php file directory file-handling

我在从另一个驱动器显示mp3文件时遇到问题。在console.log中,它显示错误Failed to load resource: the server responded with a status of 404

现在我将音乐文件存储在D:\music files 我的php文件路径是C:\wamp\www\php examples\php file handling system\files.php

现在我如何从D:\music files文件夹中获取音乐文件并将其显示在网页中。

这是我的php代码:

 <?php
    $dir_path = 'D:\music files';
    $options = '';

    if(is_dir($dir_path)){
        $files = opendir($dir_path);

        if($files){
            while(( $file_name = readdir($files)) !== FALSE){
                if($file_name != '.' && $file_name != '..'){
                    $op[] = array (
                        'name'=>$file_name
                    );
                    $options =  $options.'<option>'.$file_name.'</option>';
                    //echo $file_name."<br>";
                }
            }
        }
    }
?>

在html表格中显示

 <table class="table table-condensed table-bordered table-hover">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Name</th>
                        <th>Music</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        $k = 1;
                        foreach($op as $row){
                    ?>
                        <tr>
                            <td><?php echo $k;?></td>
                            <td><?php echo $row['name']?></td>
                            <td><audio controls>
                              <source src="<?php echo $row['name'] ?>" type="audio/mpeg">
                            Your browser does not support the audio element.
                        </audio></td>
                        </tr>
                    <?php   
                            $k++;
                        }
                    ?>
                </tbody>
            </table>

感谢提前

1 个答案:

答案 0 :(得分:0)

来自php documments:

$dir    = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);

这将列出路径'dir'中的所有文件,您可以将其放入foreach并将所有文件从音乐路径列入表格,但如果您只想获取音乐文件,则需要使用glob。 PHP DOCS::Glob function