【问题标题】:PHP validate file_exists in .m3u filePHP 验证 .m3u 文件中的 file_exists
【发布时间】:2021-06-11 09:03:03
【问题描述】:

我有一个 .m3u 文件,我正在尝试验证每一行以验证路径上的 file_exists。 这是一行:

/home/scott/Music/Whitesnake/Whitesnake (30th anniversary edition)/1-01 Still of the Night.mp3

我的代码看起来正确,但由于我已验证文件存在,因此脚本无法正常工作。我在 Google 上搜索过、作呕、堆叠和拍打,但我找不到解决方案。

先谢谢你教我...

$Jarvis  = new JarvisAPI(); // my api helper class
$m3uFile = $Jarvis->loadMainList();

array_shift( $m3uFile ); // lose the first line 

echo $Jarvis->getCount( $m3uFile ) . ' songs in list.<br/>'; 

foreach( $m3uFile as $idx => $filenamepath )
{
   if(file_exists($filenamepath)){
       echo "Exists - ";
   }else{
       echo "Not found - ";
   }

   echo $filenamepath;         
   echo "<br/>";
}

示例结果:

1116 songs in list.
Not found - /home/scott/Music/Whitesnake/Whitesnake (30th anniversary edition)/1-01 Still of the Night.mp3
Not found - /home/scott/Music/Bob Marley - The Very Best Of Bob Marley & The Wailers(2001)/Bob Marley & The Wailers - Get Up, Stand Up.mp3
Not found - /home/scott/Music/CAKE/Unknown Album/Sheep Go To Heaven.mp3

当我尝试对路径进行硬编码时,它确实有效:

$sample = '/home/scott/Music/Whitesnake/Whitesnake (30th anniversary edition)/1-01 Still of the Night.mp3';

if (file_exists($sample) ) echo filesize($sample);

输出:11736600

【问题讨论】:

  • 你是如何运行 PHP 脚本的?通过 CLI 或网络服务器?看起来是权限问题。
  • 与网络服务器根目录相关的 mp3 文件在哪里?它们是在文档根目录中还是在文件系统中的其他位置?
  • PHP 脚本在本地主机上的 /var/www/html 处运行。音乐文件(.mp3、.flac)位于同一盒子上的/home/scott/Music 目录中。
  • JarvisAPI 听起来像是钢铁侠的东西。
  • @nice_dev - 是的,我喜欢在我的许多课程中使用 Marvel :)

标签: php file-exists m3u


【解决方案1】:

基于 Windows 系统,mp3 文件位于 C 盘,并使用以下 m3u 播放列表文件内容作为playlist.m3u

#EXTM3U
#EXTINF:261,Lou Reed & Metallica - Brandenburg Gate
\data\Archives\Music\Rips\Lou Reed & Metallica - Lulu [Disc 1]\01 - Lou Reed & Metallica - Brandenburg Gate.mp3
#EXTINF:320,Lou Reed & Metallica - The View
\data\Archives\Music\Rips\Lou Reed & Metallica - Lulu [Disc 1]\02 - Lou Reed & Metallica - The View.mp3
#EXTINF:444,Lou Reed & Metallica - Pumping Blood
\data\Archives\Music\Rips\Lou Reed & Metallica - Lulu [Disc 1]\03 - Lou Reed & Metallica - Pumping Blood.mp3
#EXTINF:412,Lou Reed & Metallica - Mistress Dread
\data\Archives\Music\Rips\Lou Reed & Metallica - Lulu [Disc 1]\04 - Lou Reed & Metallica - Mistress Dread.mp3
#EXTINF:277,Lou Reed & Metallica - Iced Honey
\data\Archives\Music\Rips\Lou Reed & Metallica - Lulu [Disc 1]\05 - Lou Reed & Metallica - Iced Honey.mp3
#EXTINF:686,Lou Reed & Metallica - Cheat On Me
\data\Archives\Music\Rips\Lou Reed & Metallica - Lulu [Disc 1]\06 - Lou Reed & Metallica - Cheat On Me.mp3



<?php

    $root='c:';
    
    $file=__DIR__ . '/playlist.m3u';
    
    $lines=file( $file );
    
    for( $i=2; $i < count( $lines ); $i+=2 ){
        
        $line=$lines[ $i ];
        $path=trim( $root . $line );
        
        printf( '<div>%s</div>', file_exists( $path ) ? sprintf('The file "%s" does exist!!!',basename($path)) : sprintf('Bogus - the file "%s" cannot be found',basename($path)) );
    }
?>

这产生了以下内容:

如果不对$path 变量使用trim,这会失败,因此您或许应该尝试从$filenamepath 变量中删除尾随空格?!

【讨论】:

  • 这太他妈简单了!谢谢!!!我知道这很简单。
  • :-) 很高兴我设法为您阐明了这一点..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 2016-08-03
  • 1970-01-01
  • 2015-09-14
  • 2017-06-26
  • 2018-12-20
相关资源
最近更新 更多