PHP文件上传进度 [英] PHP file upload progress

查看:80
本文介绍了PHP文件上传进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人能够实现这个功能?

Has anybody been able to implement this functionality?

http://php.net/manual/en/session.upload-progress.php

tl;dr 是,当 POST 包含文件的表单时,如果您在 php.ini 中包含一个name"属性设置为 session.upload_progress.name 值的输入,那么 PHP 将填充 $_SESSION有关上传状态的数据.

The tl;dr is that when POSTing a form containing a file, if you include an input with the "name" attribute set to the value of session.upload_progress.name in php.ini, then PHP will populate $_SESSION with data about the status of the upload.

<form action="upload.php" method="POST" enctype="multipart/form-data">
 <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="123" />
 <input type="file" name="file1" />
 <input type="file" name="file2" />
 <input type="submit" />
</form>

上传.php

(不重要,但显然这是您通常为文件指定实际名称并将上传的文件从临时目录移动到实际目录的地方)

upload.php

(not important but obviously this is where you would normally give the file its actual name and move the uploaded file from the temp to a real directory)

<?php

print_r($_FILES);

?>

session.php:

在文件上传期间运行这个单独的 PHP 文件以检查进度

session.php:

Run this separate PHP file during the file upload to check for progress

<?php

$key = ini_get("session.upload_progress.prefix") .  $_POST[ini_get("session.upload_progress.name")];
var_dump($_SESSION[$key]);
?>

我在这些脚本和 php.ini 设置中尝试了每一个深奥的变化,无论 $_SESSION 数组是什么,都从未填充任何上传数据.默认情况下调用 Session_start().我在 v. 5.4 和 5.6 中都尝试过.我已经尝试了 PHP 文档中注释中建议的所有内容,包括使用和不使用 FastCGI 运行.我一般都试过了.

I have tried every fathomable variation in these scripts and in php.ini settings and no matter what the $_SESSION array is never populated with any upload data. Session_start() is called by default. I tried in both v. 5.4 and 5.6. I have tried everything suggested in the comments in the PHP documentation including running with and without FastCGI. I have in general tried everything.

这很重要,因为我正在开发一个桌面应用程序,该应用程序使用 POST 请求与 Web 服务器上的 .php 脚本进行通信,我没有使用浏览器,因此 JS/HTML5 解决方案不是图片的一部分.也不可能在 PHP 中编写自己的进度测量功能,因为处理文件上传 (upload.php) 的脚本在上传(到具有随机名称的临时目录)完成之前实际上不会运行.在上传完成并且上传脚本运行之前,无法找出临时文件关联的实际文件,从而允许访问 $_FILES.请帮忙,我已经尝试了所有可能的可能性.谢谢.

This is important because I am working on a desktop application that uses POST requests to communicate with .php scripts on a web server, I am not using the browser so JS/HTML5 solutions are not part of the picture. It is also impossible to write your own progress measuring functionality in PHP because the script which handles a file upload (upload.php) doesn't actually run until the upload (to the temp directory with a random name) is complete. There is no way to find out what actual file a temp is associated with until the upload is finished and the upload script runs, allowing access to $_FILES. Please help, I have tried every fathomable possibility. Thanks.

推荐答案

Form://Looks Ok

    <form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="123" />
 <input type="file" name="file1" />
 <input type="file" name="file2" />
 <input type="submit" />
</form>

Upload.php//看起来也不错

Upload.php //Looks Ok too

<?php

print_r($_FILES);

?>

Session.php//这里推荐一些改动

Session.php //recommend some changes here

<?php
session_start();//leave this out if you are session is starting automatically
$key = ini_get("session.upload_progress.prefix") ."123"; //123 value of hidden input
var_dump($_SESSION[$key]);
?>

主要挑战是在您的表单 html 视图中没有链接或调用 session.php.(我不喜欢名称 session.php 我有这部分我害怕它可能会篡改 $_SESSION 作用域).理想情况下,您可以(大多数人都这样做)使用 JS/Ajax 调用来查询 Session.php 以获得结果.在这种情况下,我建议:

The major challenge is in your form html view their is no link or call to the session.php. (i dont like the name session.php I have this part of me thats scared it may tamper with $_SESSION scoper). Ideally you can(most folks do) use JS/Ajax call to query Session.php to get result. In which case I'd recommend:

表单:(记得在此之前导入jquery)

<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="123" />
<input type="file" name="file1" />
<input type="file" name="file2" />
<input type="submit" />
</form>
<script>
$(document).ready(function(){
  $('form').on('submit',function(){
    $.get( "session.php", function( response ) {
    console.log( response ); // server response-$_SESSION dump
});
  })
})
</script>

session.php:

<?php
session_start();//leave this out if you are session is starting automatically
$key = ini_get("session.upload_progress.prefix") ."123"; //123 value of hidden input
var_dump($_SESSION[$key]);//or echo $_SESSION[$key]["bytes_processed"]. I am not sure how the array would playout inside the returned JSON
?>

这篇关于PHP文件上传进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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