如何在php中逐字节上传文件
我有一个表单输入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在上传完成(或失败)之后,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.