0
votes

I'm searching a method to convert a torrent encoding (from UTF-8-BOM to WINDOWS-1252) before download because if I download it simply with readfile() the torrent is not valid bencoding, so I need to convert it to WINDOWS-1252 or CP1252.

Click Download on my page, and download the torrent file encoded in WINDOWS-1252.

This is my script:

header("Content-Disposition: attachment; filename=\"$tname.torrent\"");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($fn));
$content = iconv("UTF-8", "WINDOWS-1252", readfile($fn));
print($content);

Acually, this script is not working because when I download the file, it's empty. What to do?

1
Why would one want to do that? Why encoding to a past world? We are happy that most systems more or less offer unicode these days. Enjoy it! Apart from that: are you sure that the content of the file can actually be encoded in such an old 8 bit encoding? I mean there is a reason for unicode: it allows to use characters you cannot describe in an 8 bit encoding. That might actually be the reason why your iconv() call fails... - arkascha
I need the encoding because: when I upload the torrent (upload.php form), the torrent is correctly uploaded and it's working - if I download it through ftp - but when I download it through download.php, torrent is not valid bencoding. I'm trying to make a torrent platform and I need that... If you can explain to me what to do... - Lego_Infinity
Sorry, I cannot help with a solution if I do not understand the issue. If the torrent is working fine, then why re-encode it? To what purpose? Wouldn't it make more sense to find out why the file cannot be used ("is not valid") after that download? What is wrong with it? How do the two versions of the file differ between the uploaded and the downloaded version? - arkascha
Look. A torrent is encoded in ANSI (Notepad++). I have GTA V torrent, if I open it with notepad++ I see it's encoded in ANSI. When I download my torrent from my own website, when I open it with Notepad++, I see it's encoded in UTF-8-BOM, and if I change it's encoding to ANSI, it's working again. - Lego_Infinity
That's why I need to re-encode the file on download. When the torrent is downloaded, it's encoded in UTF-8-BOM. I need when I download it, the file to be encoded in ANSI, not UTF-8-BOM. - Lego_Infinity

1 Answers

0
votes

readfile prints the requested file directly, but you're using it like it would return the file's contents.
For that you'll have to use file_get_contents.

You might also get the wrong file size, because a file encoded in UTF-8 might very well have a different size than one encoded in Windows-1252.
So instead of filesize($fn), use strlen($content).