5

I use the following code and function to force download files and it works great if the file name is not holding Swedish characters like Å Ä Ö.

$file_id = $_GET['f'];

$sql =  " SELECT * ".
            " FROM attachment ".
            " WHERE attachment_id = ".$file_id." ".

            $res = mysql_query($sql);
            $row = mysql_fetch_array($res);
            $filename = $row['filename'];
            $USER_ID = $row['user_id'];
            $Directory_id = $row['directory_id'];
            $target_path = "upload/".$USER_ID."/".$Directory_id."/";


function Download($path, $speed = null)
{
    if (is_file($path) === true)
    {
        $file = @fopen($path, 'rb');
        $speed = (isset($speed) === true) ? round($speed * 1024) : 524288;

        if (is_resource($file) === true)
        {
            set_time_limit(0);
            ignore_user_abort(false);

            while (ob_get_level() > 0)
            {
                ob_end_clean();
            }

            header('Expires: 0');
            header('Pragma: public');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Content-Type: application/octet-stream');
            header('Content-Length: ' . sprintf('%u', filesize($path)));
            header('Content-Disposition: attachment; filename="' . basename($path) . '"');
            header('Content-Transfer-Encoding: binary');

            while (feof($file) !== true)
            {
                echo fread($file, $speed);

                while (ob_get_level() > 0)
                {
                    ob_end_flush();
                }

                flush();
                sleep(1);
            }

            fclose($file);
        }

        exit();
    }

    return false;

    }
Download($target_path.$filename);

I tried to put this on the top of my page:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

And:

$filename = urlencode($filename); 

Or:

$filename =  htmlentities($filename, ENT_QUOTES, "UTF-8");

But still the same problem, I can't open them. But if the filename holds normal English character then it works fine.

Do you have any suggestion what can I put or implement to the function? Any help you can give will be greatly appreciated.

martynas
  • 12,120
  • 3
  • 55
  • 60
Jonas Willander
  • 432
  • 3
  • 9
  • 29

1 Answers1

0

Make sure you have utf8-encoding in your html-document (<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>)

Try to replace

$filename = $row['filename'];

with

$filename = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $row['filename']);   

So your code would look like:

$file_id = $_GET['f'];

$sql =  " SELECT * ".
            " FROM attachment ".
            " WHERE attachment_id = ".$file_id." ".

            $res = mysql_query($sql);
            $row = mysql_fetch_array($res);
            $filename = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $row['filename']);   
            $USER_ID = $row['user_id'];
            $Directory_id = $row['directory_id'];
            $target_path = "upload/".$USER_ID."/".$Directory_id."/";

But then for the sake of it. Don't use mysql()-functions. Use mysqli or PDO instead with placeholders. Your code is widely for sql-injection attacks.

bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72