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

getaudio.php version 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 version 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");
?>

if url = https://www.youtube.com/watch?v=bvYtKY-UMxA then we get $file = Deewana Kar Raha Hai Full Song 1080p HD Raaz 3 2012 YouTube-bvYtKY-UMxA.m4a

my question is getaudio.php version 1 is working fine but getaudio.php version 2 isn't....

getaudio.php version 2 is downloading but the file is coruppted....


when $file is same for both php files then how is it possible that the second one is not working


HINT: download youtube-dl.exe(for windows) & place it with the two php files & run in 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

?>

OUTPUT

Deewana Kar Raha Hai Full Song 1080p HD Raaz 3 2012 YouTube-bvYtKY-UMxA.m4a---NO SPACE HERE

Deewana Kar Raha Hai Full Song 1080p HD Raaz 3 2012 YouTube-bvYtKY-UMxA.m4a ---SPACE HERE

length of $file1 is :77

length of $file2 is :78

See that there is no whitespace after .m4a for $file1 but for $file2 there is a whitespace after .m4a.

that makes them two different string

SOLUTION: Use trim() function on $file in getaudio.php version 2

$file = trim($file);