如何在php中逐字节上传文件

发布于 2024-12-11 11:02:29 字数 809 浏览 0 评论 0原文

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

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

有什么指点吗?

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

        }
} 

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

        }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡離愁欲言轉身 2024-12-18 11:02:29

在上传完成(或失败)之后,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文