PHP large file upload, download, byte to kb

I use the easyUpload plug-in. The front-end is written according to the instructions and the back-end writes a receiving interface.

I am using thinkphp5

    public function upload(){
        $file = Request::instance()->file('file');
        //给定目录
//        var_dump($file);die;
        $info = $file->move('upload');
        var_dump($info);die;
        if($info && $info->getPathname()){
        	// echo $info->getPathname();die;
            return show(200,'success','/'.$info->getPathname());
        }
        return show(0,'upload error');
    }


function show($status, $message,$data=[]){
    return [
        'code'=> intval($status),
        'message'=>$message,
        'data'=>$data,
    ];
}

The above is the default json format output set in the backend program config.

Under expansion, you can get information such as file size in $info.

The above content is normal to receive pictures and so on, but once it encounters large files, it will fail.

Because php defaults to limit the memory, time, upload files, etc. of each page. The php.ini file needs to be modified to ensure large file uploads.

upload_max_filesize = 8m ; The maximum allowed upload file size. The default is 2M
post_max_size = 8m ; the maximum value that can be received by form POST to PHP. Default is 8M

max_execution_time = 30; the maximum time value (seconds) for each PHP page to run, default 30 seconds
max_input_time = 60; the maximum time required for each PHP page to receive data, default 60 seconds
memory_limit = 8m; eaten by each PHP page The maximum memory, default 8M

Change the above content to the actual number of items required, and restart apache.

 

--------------------------download----------------------- --

The download is actually relatively simple, you can directly download it at <a href="link address">download<a>, but this is not very safe and is too exposed.

It would be better to transfer all downloads to a unified page for security verification and release.

The id is the unique identifier of the file, the token is a protection measure, and the download is provided if the verification is passed. The method is written at will and can be expanded a lot, which can verify the login, ip, session and other verification.

public function download($id,$token){
        if(!is_numeric($id) && $token != $this->token){
            die('error009');
        }

        $file_info = model('GameVersion')->getOneData('id = '.$id);//获取文件信息
        $file_url = '.'.$file_info['down_link'];
        $new_name = $file_info['resource_name'];
        if(!isset($file_url)||trim($file_url)==''){
            echo '500';
            die('error011');
        }
        if(!file_exists($file_url)){ //检查文件是否存在
            echo '404';
            die('error010');
        }

        $file_name=basename($file_url);//获取文件名
        $file_type=explode('.',$file_url);
        $file_type=$file_type[count($file_type)-1];//获取类型
        $file_name = $new_name.'.'.$file_type;//设置名称类型
        $file=fopen($file_url,'r'); //打开文件

        //输入文件标签
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: ".$file_info['file_size']);
        header("Content-Disposition: attachment; filename=".$file_name);

        //输出文件内容
        echo fread($file,filesize($file_url));
        fclose($file);
    }

 

In addition, requirements such as byte-to-kb or mb may be used here, and they are written here.

function kb($filesize){
    if ($filesize >= 1073741824) {
        $filesize = round($filesize / 1073741824 * 100) / 100 .' GB';
    } elseif ($filesize >= 1048576) {
        $filesize = round($filesize / 1048576 * 100) / 100 .' MB';
    } elseif($filesize >= 1024) {
        $filesize = round($filesize / 1024 * 100) / 100 . ' KB';
    } else {
        $filesize = $filesize.' Bytes';
    }
    return $filesize;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325940411&siteId=291194637