Home > Backend Development > PHP Tutorial > The size of the downloaded file in the php file download function is inconsistent with the size of the server source file

The size of the downloaded file in the php file download function is inconsistent with the size of the server source file

WBOY
Release: 2016-08-04 09:19:01
Original
2106 people have browsed it

1. Based on the function of downloading files from the Internet

<code>    public function putFile($file_dir, $file_name){
        $file_dir = chop($file_dir);//去掉路径中多余的空格
        //得出要下载的文件的路径
        if($file_dir != '')
        {
            $file_path = $file_dir;
            if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
                $file_path .= '/';
            $file_path .= $file_name;
        }
        else
            $file_path = $file_name;

        //判断要下载的文件是否存在
        if(!file_exists($file_path))
        {
         //   alert('对不起,你要下载的文件不存在');
            return false;
        }

        $file_size = filesize($file_path);

        header("Content-type: application/octet-stream;charset=gbk");
        header("Accept-Ranges: bytes");
        header("Accept-Length: $file_size");
        header("Content-Disposition: attachment; filename=".$file_name);

        $fp = fopen($file_path,"r");
        $buffer_size = 1024;
        $cur_pos = 0;

        while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
        {
            $buffer = fread($fp,$buffer_size);
            echo $buffer;
            $cur_pos += $buffer_size;
        }

        $buffer = fread($fp,$file_size-$cur_pos);
        echo $buffer;
        fclose($fp);
        return true;
    }</code>
Copy after login
Copy after login

I am sure it is the same file but the downloaded file appears to be smaller than the source file. Why?

Reply content:

1. Based on the function of downloading files from the Internet

<code>    public function putFile($file_dir, $file_name){
        $file_dir = chop($file_dir);//去掉路径中多余的空格
        //得出要下载的文件的路径
        if($file_dir != '')
        {
            $file_path = $file_dir;
            if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
                $file_path .= '/';
            $file_path .= $file_name;
        }
        else
            $file_path = $file_name;

        //判断要下载的文件是否存在
        if(!file_exists($file_path))
        {
         //   alert('对不起,你要下载的文件不存在');
            return false;
        }

        $file_size = filesize($file_path);

        header("Content-type: application/octet-stream;charset=gbk");
        header("Accept-Ranges: bytes");
        header("Accept-Length: $file_size");
        header("Content-Disposition: attachment; filename=".$file_name);

        $fp = fopen($file_path,"r");
        $buffer_size = 1024;
        $cur_pos = 0;

        while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
        {
            $buffer = fread($fp,$buffer_size);
            echo $buffer;
            $cur_pos += $buffer_size;
        }

        $buffer = fread($fp,$file_size-$cur_pos);
        echo $buffer;
        fclose($fp);
        return true;
    }</code>
Copy after login
Copy after login

I am sure it is the same file but the downloaded file appears to be smaller than the source file. Why?

It’s normal for different systems to have differences

It’s too complicated to write. The more complicated it is, the easier it is to make mistakes. I’m not sure if there is a problem with your length management. The following code is enough.

<code>$fp = fopen($file_path,"r");
$content = '';
while($_content = fread($fp, 1024)) $content.= $_content;
fclose($fp);</code>
Copy after login

If the file is only 100 bytes, fread($fp, 1024) will not get 1024 bytes.

Also...I think charset=gbk should be removed, and there is no need to specify the encoding.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template