douxu5233 2015-03-17 21:28
浏览 78
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • dstwfcz1377 2015-03-20 12:03
    关注
    <?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); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog