doushi3189 2012-08-13 04:31
浏览 16
已采纳

PHP Scrape Mp3文件

I am embedding some mp3 files on my website. The problem is some of the names are different and my website does not know how to embed - unless I define all the files with the correct names.

For example,

http://example.com/chapter1/book-[some random name].001.mp3

I have set up my website so it embeds like this

http://example.com/chapter1/book.001.mp3

Is there any possible solution that I can use with php so it auto fills the [some random name].

  • 写回答

1条回答 默认 最新

  • dpqjvoq9033 2012-08-13 05:47
    关注

    You have some options,

    1. Output the path to the file correctly (easiest)

    2. Build and store a registry of the mp3s in a db or flat file and use mod_rewrite to pass the parameters to a loader script. (Example below)

    <?php 
    /* mod_rewrite  .htaccess in chapters folder
    RewriteEngine On
    Options -Indexes
    RewriteBase /chapter1
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*).mp3$ index.php?loadmp3=$1 [L]
    */
    $mp3_dir='./mp3s/';
    //Example passed url: http://localhost/chapter1/book.002.mp3
    if(isset($_GET['loadmp3'])){
        $real_path = get_mp3_list($_GET['loadmp3']);
        if($real_path != false){
            //Result:  ./mp3s/book-next_random_name.002.mp3
            print_r($real_path);
            //Pass the url to a streamer script
        }else{
            //not found
        }
    }
    
    /**
     * Build & cache, search for mp3 from array
     */
    function get_mp3_list($search){
        global $mp3_dir;
        if(file_exists($mp3_dir.'mp3s.json')){
            $list = json_decode(file_get_contents($mp3_dir.'mp3s.json'),true);
        }else{
            $mp3s = glob($mp3_dir."*.mp3");
            $list = array();
            foreach($mp3s as $mp3){
                if(preg_match("#(\w+)-(\w+).(\d+).mp3#", $mp3, $match)){
                    $list[]=array('location'=>$mp3,
                    'type'=>$match[1],
                    'name'=>$match[2],
                    'episode'=>$match[3]);
                }
                if(!empty($list)){file_put_contents($mp3_dir.'mp3s.json', json_encode($list));}
            }
        }
    
        $search = explode('.',$search,2);
        foreach($list as $mp3){
            if($mp3['type'] == $search[0] && $mp3['episode'] == $search[1]){
                return $mp3['location'];
            }
        }
        return false;
    }
    /*
    $list Example Array 
    (
        [0] => Array
            (
                [location] => ./mp3s/book-next_random_name.002.mp3
                [type] => book
                [name] => next_random_name
                [episode] => 002
            )
    
        [1] => Array
            (
                [location] => ./mp3s/book-some_random_name.001.mp3
                [type] => book
                [name] => some_random_name
                [episode] => 001
            )
    
    )
    */
    ?>
    

    Hope it helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么