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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。