How to upload a file byte by byte in php

回眸只為那壹抹淺笑 提交于 2019-12-13 21:58:42

问题


I have a form input type="file" element and it accepts a file . When I upload it and pass this on to the server side php script .

How do I write the temporary file stored in $_FILES["file"]["tmp_name"] byte by byte ? The below code does not work . It seems to write at the end of the request . IF for eg a connection is lost in between i would like to see if 40 % was complete so that i can resume it .

Any pointers ?

$target_path = "uploads/";
$target_path = $target_path . basename($name);
if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
        // Open temp file
        $out = fopen($target_path, "wb");
        if ($out) {
            // Read binary input stream and append it to temp file
            $in = fopen($_FILES['file']['tmp_name'], "rb");

            if ($in) {
                while ($buff = fread($in, 4096))
                    fwrite($out, $buff);

            } 

            fclose($in);
            fclose($out);

        }
} 

回答1:


PHP does not hand over control to the file upload target script until AFTER the upload is complete (or has failed). You don't have to do anything to 'accept' the file - PHP and Apache will take care of writing it to the filename specified in the ['tmp_name'] parameter of the $_FILES array.

If you're trying to resume failed uploads, you'll need a much more complicated script.



来源:https://stackoverflow.com/questions/7840503/how-to-upload-a-file-byte-by-byte-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!