如何在php中逐字节上传文件 [英] How to upload a file byte by byte in php

查看:45
本文介绍了如何在php中逐字节上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单输入 type="file" 元素,它接受一个文件.当我上传它并将其传递给服务器端 php 脚本时.

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 .

如何逐字节写入存储在 $_FILES["file"]["tmp_name"] 中的临时文件?下面的代码不起作用.它似乎写在请求的末尾.例如,如果中间的连接丢失,我想看看 40% 是否已完成,以便我可以恢复它.

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 .

有什么指点吗?

$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);

        }
} 

推荐答案

在上传完成(或失败)之前,PHP 不会将控制权移交给文件上传目标脚本.您无需执行任何操作即可接受"文件 - PHP 和 Apache 将负责将其写入 $_FILES 数组的 ['tmp_name'] 参数中指定的文件名.

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.

这篇关于如何在php中逐字节上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆