简体   繁体   English

PHP 验证 file_exists in.m3u 文件

[英]PHP validate file_exists in .m3u file

I have a.m3u file, and I'm trying to validate each line to validate file_exists on the path.我有一个 .m3u 文件,我正在尝试验证每一行以验证路径上的file_exists Here's a single line:这是一行:

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

My code looks correct, but the script is not working as I have validated the files exist.我的代码看起来是正确的,但由于我已验证文件存在,因此脚本无法正常工作。 I've googled, gaggled, Stacked' and smacked, and I'm coming up empty for a solution.我已经用谷歌搜索了,gggled,Stacked' 和 smacked,但我正在寻找一个解决方案。

Thank you in advance for teaching me...提前谢谢你教我...

$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/>";
}

Sample results:样本结果:

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

When I try hardcoding a path, it works, though:但是,当我尝试对路径进行硬编码时,它可以工作:

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

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

outputs: 11736600输出:11736600

Based upon a Windows system with mp3 files located on the C drive and using the following m3u playlist file contents as playlist.m3u基于 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)) );
    }
?>

This generated the following:这产生了以下内容:

在此处输入图像描述

Without using trim on the $path variable this failed so you perhaps should try removing trailing spaces from your $filenamepath variable?!如果不对$path变量使用trim则会失败,因此您可能应该尝试从$filenamepath变量中删除尾随空格?!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM