php后端分块接收文件

php


    public function fileuploadSection()
    {
        $user_id = $_REQUEST["userID"];
        $user_token = $_REQUEST["userToken"];
        //用户校验

        $file_md5 = $_REQUEST['fileMd5'];
        $file_name = $_REQUEST['fileName'];
        $file_offset = $_REQUEST['fileOffset'];
        $file_total_size = $_REQUEST['fileTotalSize'];
        $is_end = $_REQUEST['isEnd'];

        $binary_file_str = 'binaryFile';

        $file_section_size = $_FILES[$binary_file_str]['size'];
        if ($file_section_size <= 0) {
            return 1;
        }

        $file_path = "/tmp/" . $file_md5 . "_" . $file_total_size;
        if(0==$file_offset){
            if(file_exists($file_path)){
                @unlink($file_path);
            }
            chmod('/tmp/', 0777);
        }

        $exist_file_size = filesize($file_path);
        if($file_offset!=$exist_file_size){
            return 2;
        }

        if (!file_put_contents($file_path, file_get_contents($_FILES[$binary_file_str]['tmp_name']), FILE_APPEND)) {
            return 3;
        }

        if($is_end){
            if($exist_file_size + $file_section_size != $file_total_size){
                return 4;
            }

            $merge_file_md5 = md5_file($file_path);
            if($file_md5!=$merge_file_md5){
                return 5;
            }
        }

        return 0;
    }

分段接收到文件后,写入指定的临时文件,接收完成后,移动到指定位置进行处理

发布了275 篇原创文章 · 获赞 46 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/youyudexiaowangzi/article/details/102683397