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

时间:2015-03-17 21:28:24

标签: php youtube youtube-dl

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");
?>

如果 网址 = 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 正在下载,但该文件已被损坏....


当两个php文件的 $ file 相同时,第二个文件怎么可能无法正常工作


提示:下载 youtube-dl.exe (适用于Windows)&amp;使用两个php 文件&amp ;; 放置在localhost中运行

1 个答案:

答案 0 :(得分:1)

<?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

&GT;

输出

  

Deewana Kar Raha Hai Full Song 1080p HD Raaz 3 2012   YouTube-bvYtKY-UMxA.m4a ---这里没有空间

     

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

     

$ file1的长度是:77

     

$ file2的长度是:78

看到 $ file1 .m4a 之后没有空格,但对于 $ file2 .m4a后面有一个空格

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

解: 在 getaudio.php版本2

中的$ file上使用trim()函数
$file = trim($file);