PHP 上传进度条

发布于 2024-07-19 18:37:17 字数 99 浏览 7 评论 0原文

有谁知道如何在 php 中获取上传进度条? 我正在尝试为相册上传器编写代码。 我希望在上传照片时显示进度条。

我对 php 相当陌生,所以我不了解它的一切。

Does anyone know how to get a progress bar for an upload in php? I am trying writing code for a photo album uploader. I would like a progress bar to display while the photos are uploading.

I am fairly new to php so I dont know everything about it.

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

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

发布评论

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

评论(10

幻想少年梦 2024-07-26 18:37:17

这是迄今为止(经过几个小时的谷歌搜索和尝试脚本)我发现的最简单的设置和最好的上传器

https://github.com/FineUploader/fine-uploader

它不需要 APC 或任何其他外部 PHP 库,我可以在共享主机上获取文件进度反馈,并且它声称支持 html5 拖放(个人未经测试)和多个文件上传。

This is by far (after hours of googling and trying scripts) the simplest to set up and nicest uploader I've found

https://github.com/FineUploader/fine-uploader

It doesn't require APC or any other external PHP libraries, I can get file progress feedback on a shared host, and it claims to support html5 drag and drop (personally untested) and multiple file uploads.

请帮我爱他 2024-07-26 18:37:17

如果您安装了APC,它有一个用于上传进度的回调挂钩。

Rasmus Lerdorf(PHP 的创建者)有一个使用 YUI 的 示例(哦,这是 PHP 源)。

请参阅此处的文档

If you have APC installed, it has a callback hook for upload progress.

Rasmus Lerdorf (PHP's creator) has a sample of this using YUI (oh, and here's the PHP source).

See the documentation here.

绾颜 2024-07-26 18:37:17

我很遗憾地说,据我所知,由于 PHP 的工作原理,纯 PHP 上传进度条,甚至 PHP/Javascript 上传进度条都是不可能的。 最好的选择是使用某种形式的 Flash 上传器。

AFAIK 这是因为在填充所有超全局变量(包括 $_FILES)之前,您的脚本不会执行。 当您的 PHP 脚本被调用时,文件已完全上传。

编辑:这不再是真的。 那是在2010年。

I'm sorry to say that to the best of my knowledge a pure PHP upload progress bar, or even a PHP/Javascript upload progress bar is not possible because of how PHP works. Your best bet is to use some form of Flash uploader.

AFAIK This is because your script is not executed until all the superglobals are populated, which includes $_FILES. By the time your PHP script gets called, the file is fully uploaded.

EDIT: This is no longer true. It was in 2010.

谁与争疯 2024-07-26 18:37:17

一个 PHP-ish (5.2+) & 无 Flash 方式对我来说效果很好:

首先,请参阅 this 帖子解释了如何启动并运行“uploadprogress”扩展。

然后,在包含要从中上传文件的表单的页面中,创建以下 iframe:

<iframe id="progress_iframe" src="" style="display:none;" scrolling="no" frameborder="0"></iframe>

接下来,将此代码添加到“提交”按钮中:

onclick="function set() { f=document.getElementById('progress_iframe'); f.style.display='block'; f.src='uploadprogress.php?id=<?=$upload_id?>';} setTimeout(set);"

现在您的表单中有一个隐藏的 iframe,它将变得可见并显示当您点击“提交”开始上传文件时,uploadprogress.php 的内容。 $upload_id 必须与您在表单中用作隐藏字段“UPLOAD_IDENTIFIER”的值相同。

uploadprogress.php 本身看起来像这样(根据您的需要进行修复和调整):

<html>
<head>
<META HTTP-EQUIV='REFRESH' CONTENT='1;URL=?id=<?=$_GET['id']?>'>
</head>
<body>
Upload progress:<br />
<?php
    if(!$_GET['id']) die;
    $info = uploadprogress_get_info($_GET['id']);
    $kbytes_total = round($info['bytes_total'] / 1024);
    $kbytes_uploaded = round($info['bytes_uploaded'] / 1024);
    echo $kbytes_uploaded.'/'.$kbytes_total.' KB';
?>
</body>
</html>

请注意,它每秒都会自我刷新。 如果您愿意,您当然可以在此处添加一些漂亮的视觉进度条(例如 2 个具有不同颜色的嵌套

)。 具有上传进度的 iframe 自然只在上传过程中起作用,并在表单提交且浏览器重新加载到下一页后结束其可见生命。

One PHP-ish (5.2+) & no-Flash way that worked nicely for me:

First, see this post explaining how to get "uploadprogress" extension up and running.

Then, in the page containing the form that you are uploading file(s) from, create the following iframe:

<iframe id="progress_iframe" src="" style="display:none;" scrolling="no" frameborder="0"></iframe>

Next, add this code to your "Submit" button:

onclick="function set() { f=document.getElementById('progress_iframe'); f.style.display='block'; f.src='uploadprogress.php?id=<?=$upload_id?>';} setTimeout(set);"

Now you have a hidden iframe in your form that will come visible and show contents of uploadprogress.php when you click "Submit" to start uploading files. $upload_id must be the same that you are using as the value of hidden field "UPLOAD_IDENTIFIER" in your form.

The uploadprogress.php itself looks about like this (fix and adjust to your needs):

<html>
<head>
<META HTTP-EQUIV='REFRESH' CONTENT='1;URL=?id=<?=$_GET['id']?>'>
</head>
<body>
Upload progress:<br />
<?php
    if(!$_GET['id']) die;
    $info = uploadprogress_get_info($_GET['id']);
    $kbytes_total = round($info['bytes_total'] / 1024);
    $kbytes_uploaded = round($info['bytes_uploaded'] / 1024);
    echo $kbytes_uploaded.'/'.$kbytes_total.' KB';
?>
</body>
</html>

Note that is self-refreshes every second. You can surely add some nice visual progress bar here (like 2 nested <div>s with different colors) if you like. The iframe with upload progress naturally only works while the upload is in progress, and ends its visible life once the form is submitted and browser reloads to the next page.

仙女 2024-07-26 18:37:17

上传进度条的实现很简单,不需要任何额外的 PHP 扩展、JavaScript 或 Flash。 但是您需要 PHP 5.4 及更高版本

您必须通过设置指令 session.upload_progress.enabledphp.ini 中的On

然后将隐藏输入添加到 HTML 上传表单中,位于任何其他文件输入之前。 该隐藏输入的 HTML 属性 name 应该与指令 session.upload_progress.name 来自 php.ini (最终前面是 session.upload_progress.prefix)。 value 属性由您决定,它将用作会话密钥的一部分。

HTML 表单可能如下所示:

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

当您发送此表单时,PHP 应在 $_SESSION 超全局结构中创建一个新键,该键将填充上传状态信息。 键是隐藏输入的namevalue 连接而成。

在 PHP 中,您可以查看填充的上传信息:

var_dump($_SESSION[
    ini_get('session.upload_progress.prefix')
   .ini_get('session.upload_progress.name')
   .'_myupload'
]);

输出将类似于以下内容:

$_SESSION["upload_progress_myupload"] = array(
  "start_time" => 1234567890,   // The request time
  "content_length" => 57343257, // POST content length
  "bytes_processed" => 54321,   // Amount of bytes received and processed
  "done" => false,              // true when the POST handler has finished, successfully or not
  "files" => array(
    0 => array(
      "field_name" => "file1",    // Name of the <input /> field
      // The following 3 elements equals those in $_FILES
      "name" => "filename.ext",
      "tmp_name" => "/tmp/phpxxxxxx",
      "error" => 0,
      "done" => false,            // True when the POST handler has finished handling this file
      "start_time" => 1234567890, // When this file has started to be processed
      "bytes_processed" => 54321, // Number of bytes received and processed for this file
    )
  )
);

有创建进度条所需的所有信息 - 您拥有上传是否仍在进行中的信息、有多少字节的信息总共要传输多少字节以及已经传输了多少字节。

要向用户展示上传进度,请编写另一个 PHP 脚本而不是上传脚本,该脚本只会查看会话中的上传信息并以 JSON 格式返回。 该脚本可以使用 AJAX 和呈现给用户的信息定期调用,例如每秒调用一次。

您甚至可以通过将 $_SESSION[$key]['cancel_upload'] 设置为 true 来取消上传。

有关详细信息、其他设置和用户评论,请参阅 PHP 手册

Implementation of the upload progress bar is easy and doesn't require any additional PHP extension, JavaScript or Flash. But you need PHP 5.4 and newer.

You have to enable collecting of the upload progress information by setting the directive session.upload_progress.enabled to On in php.ini.

Then add a hidden input to the HTML upload form just before any other file inputs. HTML attribute name of that hidden input should be the same as the value of the directive session.upload_progress.name from php.ini (eventually preceded by session.upload_progress.prefix). The value attribute is up to you, it will be used as part of the session key.

HTML form could looks like this:

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

When you send this form, PHP should create a new key in the $_SESSION superglobal structure which will be populated with the upload status information. The key is concatenated name and value of the hidden input.

In PHP you can take a look at populated upload information:

var_dump($_SESSION[
    ini_get('session.upload_progress.prefix')
   .ini_get('session.upload_progress.name')
   .'_myupload'
]);

The output will look similarly to the following:

$_SESSION["upload_progress_myupload"] = array(
  "start_time" => 1234567890,   // The request time
  "content_length" => 57343257, // POST content length
  "bytes_processed" => 54321,   // Amount of bytes received and processed
  "done" => false,              // true when the POST handler has finished, successfully or not
  "files" => array(
    0 => array(
      "field_name" => "file1",    // Name of the <input /> field
      // The following 3 elements equals those in $_FILES
      "name" => "filename.ext",
      "tmp_name" => "/tmp/phpxxxxxx",
      "error" => 0,
      "done" => false,            // True when the POST handler has finished handling this file
      "start_time" => 1234567890, // When this file has started to be processed
      "bytes_processed" => 54321, // Number of bytes received and processed for this file
    )
  )
);

There is all the information needed to create a progress bar — you have the information if the upload is still in progress, the information how many bytes is going to be transferred in total and how many bytes has been transferred already.

To present the upload progress to the user, write an another PHP script than the uploading one, which will only look at the upload information in the session and return it in the JSON format, for example. This script can be called periodically, for example every second, using AJAX and information presented to the user.

You are even able to cancel the upload by setting the $_SESSION[$key]['cancel_upload'] to true.

For detailed information, additional settings and user's comments see PHP manual.

拍不死你 2024-07-26 18:37:17

另一个上传器完整的JS: http:// Developers.sirika.com/mfu/

  • 它是免费的(BSD许可证)
  • 国际化的
  • 跨浏览器兼容
  • 您可以选择是否安装APC(不确定进度条VS确定进度条)
  • 可定制的外观,因为它使用dojo模板机制。 您可以根据您的 CSS 在 te 模板中添加您的类/Ids

玩得开心

Another uploader full JS : http://developers.sirika.com/mfu/

  • Its free ( BSD licence )
  • Internationalizable
  • cross browser compliant
  • you have the choice to install APC or not ( underterminate progress bar VS determinate progress bar )
  • Customizable look as it uses dojo template mechanism. You can add your class / Ids in the te templates according to your css

have fun

夜清冷一曲。 2024-07-26 18:37:17

HTML5 引入了文件上传 api,允许您监控上传进度文件上传,但对于较旧的浏览器,有 plupload 一个专门用于监视文件上传并提供有关信息的框架他们。 另外它有大量的回调,因此它可以在所有浏览器上工作

HTML5 introduced a file upload api that allows you to monitor the progress of file uploads but for older browsers there's plupload a framework that specifically made to monitor file uploads and give information about them. plus it has plenty of callbacks so it can work across all browsers

清风夜微凉 2024-07-26 18:37:17

Gears 和 HTML5 在 HttpRequest 对象中有一个进度事件,用于通过 AJAX 提交文件上传。

http://developer.mozilla.org/en/Using_files_from_web_applications

您的其他选项已回答其他是:

  1. 基于 Flash 的上传器。
  2. 基于 Java 的上传器。
  3. 第二个comet-style请求网络服务器或脚本来报告大小收到的数据。 一些网络服务器(例如 Lighttpd)提供了在进程内执行此操作的模块,以节省调用外部脚本或进程的开销。

从技术上讲,还有第四种选项,类似于 YouTube 上传,通过 Gears 或 HTML5,您可以使用 blob 将文件分割成小块,然后单独上传每个块。 完成每个块后,您可以更新进度状态。

Gears and HTML5 have a progress event in the HttpRequest object for submitting a file upload via AJAX.

http://developer.mozilla.org/en/Using_files_from_web_applications

Your other options as already answered by others are:

  1. Flash based uploader.
  2. Java based uploader.
  3. A second comet-style request to the web server or a script to report the size of data received. Some webservers like Lighttpd provide modules to do this in-process to save the overhead of calling an external script or process.

Technically there is a forth option, similar to YouTube upload, with Gears or HTML5 you can use blobs to split a file into small chunks and individually upload each chunk. On completion of each chunk you can update the progress status.

套路撩心 2024-07-26 18:37:17

您需要使用 Javascript 创建进度条。 简单的 Google 搜索让我找到了这篇文章: WebAppers 带有 CSS 的简单 Javascript 进度条

Dojo 文件上传进度栏小部件 是使用 Dojo 的另一个选项JavaScript 框架。

编辑:假设您上传大量图像(例如相册),并将它们发布到 PHP 脚本中,您可以使用 javascript 从帖子中读回结果,并根据上传的图像数量更新进度条/ 图像总数。 这具有仅在每个帖子完成后更新的副作用。 请查看此处有关如何使用 JS 发帖的一些信息。

You would need to use Javascript to create a progress bar. A simple Google search led me to this article: WebAppers Simple Javascript Progress Bar with CSS.

Dojo File Upload Progress Bar Widget is another option using the Dojo Javascript framework.

EDIT: Assuming your uploading a large number of images (such as a photo album), and POSTing them to your PHP script, you could use javascript to read the results back from the post and update the progress bar based on the number of images uploaded / total number of images. This has the side effect of only updating after each post has completed. Check out here for some info on how to post with JS.

假面具 2024-07-26 18:37:17

可以做一个php/ajax进度条。 (查看 pear 中的 Html_Ajax 库)。
然而,这需要将自定义模块安装到 php.ini 中。

其他方法需要使用 iframe,php 通过该 iframe 来查看已上传的文件量。 然而,这种隐藏的 iframe 可能会被某些浏览器插件阻止,因为隐藏的 iframe 通常用于向用户计算机发送恶意数据。

如果您无法控制服务器,最好的选择是使用某种形式的 Flash 进度条。

A php/ajax progress bar can be done. (Checkout the Html_Ajax library in pear).
However this requires installing a custom module into php.

Other methods require using an iframe, through which php looks to see how much of the file has been uploaded. However this hidden iframe, may be blocked by some browsers addons because hidden iframes are often used to send malicious data to a users computer.

Your best bet is to use some form of flash progress bar if you do not have control over your server.

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