doonbfez815298 2015-10-09 22:44
浏览 57
已采纳

来自fopen的file_put_contents创建的文件与浏览器不同

This is driving me crazy. I can download a file from internet through browser and its working fine. However if I write a script that does this for me using PHP, the file ends up being completely different (as per online diff tools).

$link = "https://torcache.net/torrent/9AE1726935FF9C08DF422CCE3C4445FC9484478B.torrent?title=[kat.cr]the.big.bang.theory.s08e24.720p.hdtv.x264.dimension.rartv";

file_put_contents($file_name, fopen( $link, 'r'));

First I tried to play around with encoding, but that should not matter as the file is binary, right? Also tried file_get_contents instead of fopen first, the same problem.

My PHP app is running on UTF-8 w/o BOM files. Can someone help? What am I doing wrong?

  • 写回答

3条回答 默认 最新

  • dongshike7171 2015-10-09 23:09
    关注

    I think I got what you mean. The responses from torcache.net are all compressed with gzip. If you download the file from your browser, the file is automatically decoded, but if you do the same with php you get the same file but still encoded. You can use gzdecode to decodes it.

    file_put_contents($file_name, gzdecode(file_get_contents($link)));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?