Hi,
I'm unable to download a file that is having a 2-byte char in its name (e.g.テ) using force download option. Though, am able to download file names involving ASCII chars. I have tried URL encoding too, but with no success.
Can someone provide details on how to handle the 2-byte char URLs and download the files?
Appreciate your suggestions/help in resolving it.
Here is my code:
<?php
function readfile_chunked($filename,$retbytes=true)
{
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
$handle = fopen($filename, 'rb');
if ($handle === false)
{
return false;
}
while (!feof($handle))
{
//read a chunk
$buffer = fread($handle, $chunksize);
//send the chunk
echo $buffer;
//flush the chunk
flush();
//increment the size read/sent
if ($retbytes)
{
$cnt += strlen($buffer);
}
}
//close file
$status = fclose($handle);
if ($retbytes && $status)
{
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
$filename = $_GET['file'];
if(!$filename)
{
echo "ERROR: No filename specified. Please try again.";
}
else
{
// fix for IE caching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=" . basename($filename) . ";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($filename));
set_time_limit(0);
readfile_chunked(urldecode($filename), false);
exit();
}
?>
Thanks,
-Venkat