两个代码理论上相同,但实际工作方式不同..这怎么可能


two codes Theoreticaly same but practicaly works different...how is it possible

getaudio.php 版本1

<?php
    $youtubeUrl =  $_GET['url'];
    $content = shell_exec("youtube-dl -j $youtubeUrl "); 
    $meta=json_decode($content);  
    $file= $meta->{'_filename'};
    $fileWithoutExtension = explode(".",$file)[0];
    $extension = ".m4a";
    $file = $fileWithoutExtension . $extension;   
    header("Content-Disposition: attachment; filename='"$file'"" );
    header("Content-Type: application/octet-stream");
    passthru("youtube-dl -f 140 -o - $youtubeUrl");     
?>

getaudio.php版本2

<?php
    $youtubeUrl =  $_GET['url'];
    $file = shell_exec("youtube-dl -f 140 --get-filename $youtubeUrl "); 
    header("Content-Disposition: attachment; filename='"$file'"" );
    header("Content-Type: application/octet-stream");
    passthru("youtube-dl -f 140 -o - $youtubeUrl");
?>

如果url=https://www.youtube.com/watch?v=bvYtKY-UMxA,然后我们得到$file=Deewana Kar Raha Hai全曲1080p高清Raaz 3 2012 YouTube-bvYtKY-UMxA.m4a

我的问题是getaudio.php版本1运行良好,但getaudio.php第2不正常。。。。

getaudio.php版本2正在下载,但该文件已升级。。。。


$file对于两个php文件都相同时,第二个文件怎么可能不工作


提示:下载youtube dl.exe(适用于windows)&将它与两个php文件一起放置;在localhost 中运行

<?php
$youtubeUrl =  "https://www.youtube.com/watch?v=bvYtKY-UMxA";
$content = shell_exec("youtube-dl -j $youtubeUrl "); 
$meta=json_decode($content);  
$file= $meta->{'_filename'};
$fileWithoutExtension = explode(".",$file)[0];
$extension = ".m4a";
$file1 = $fileWithoutExtension . $extension;   
$file2 = shell_exec("youtube-dl -f 140 --get-filename $youtubeUrl ");
echo $file1,"NO SPACE HERE"; 
echo "<br>";    
 // have extra space at the end 
echo $file2,"SPACE HERE";        
echo "<br>"; 
echo "length of '$file1 is :".strlen($file1);  // 77
echo "<br>"; 
echo "length of '$file2 is :".strlen($file2);  // 78

?>

输出

Deewana Kar Raha Hai全曲1080p高清Raaz 3 2012YouTube-bvYtKY-UMxA.m4a-此处无空格

Deewana Kar Raha Hai全曲1080p高清Raaz 3 2012YouTube-bvYtKY-UMxA.m4a——此处空白

$file1的长度为:77

$file2的长度为:78

请注意,对于$file1.m4a之后没有空白,但对于$file2来说,.m4a之后有空白。

这使它们成为两个不同的字符串

解决方案:在getaudio.php版本2中的$file上使用trim()函数

$file = trim($file);