使用PHP获取上传进度

编程入门 行业动态 更新时间:2024-10-12 03:19:14
本文介绍了使用PHP获取上传进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法使用PHP获取文件上传的进度,我的网站托管的服务器正在使用php v5.6.

我也在cpanel管理器中启用了"uploadprogress"设置.

我尝试按照本教程进行操作: 使用PHP和JavaScript跟踪上传进度

但是我将其与其他一些代码合并在一起,这些代码使用ajax请求来处理文件的上载.

我的表单代码:

<form id="myForm"> <input type="hidden" value="myForm" name="<?php echo ini_get("session.upload_progress.name"); ?>"> <input type="file" id="file" /> <a href="javascript:uploadFile()">Upload</a> </form> Upload: <div id="process">0%</div>

JavaScript代码: var timer;

function uploadFile() { var uploadFile = $("#file")[0].files[0]; timer = setInterval(getStatus,1000); $.ajax( { url : "upload.php", type: 'POST', data : new FormData($('form')[0]), cache: false, contentType: false, processData: false, xhr: function() { var myXhr = $.ajaxSettings.xhr(); if (myXhr.upload) { // For handling the progress of the upload myXhr.upload.addEventListener('progress', function(e) { if (e.lengthComputable) { if (e.loaded < e.total) { $('#process').html(((e.loaded / e.total) * 100).toFixed(1) + "%"); } else { $('#process').html("Processing upload, please wait."); } } } , false); } return myXhr; }, success: function (response) { $('#process').html(response); clearTimeout(timer); } } ); } function getStatus() { $.get("progress.php", function(response) { $("#process2").html(response); } ); }

upload.php

session_start(); $uploadedFile = $_FILES["file"]; move_uploaded_file($uploadedFile['tmp_name'],$uploadedFile["name"]); echo $uploadedFile["name"]." was uploaded";

progress.php

session_start(); $key = ini_get("session.upload_progress.prefix") . "myForm"; if (!empty($_SESSION[$key])) { $current = $_SESSION[$key]["bytes_processed"]; $total = $_SESSION[$key]["content_length"]; echo $current < $total ? ceil($current / $total * 100) : 100; } else { echo 100; }

不幸的是,在我的JS行中: $('#process').html((((e.loaded/e.total)* 100).toFixed(1)+%");

仅获取获取请求所花费的时间,而不实际获取文件.

希望我能提供足够的信息以获取帮助.

解决方案

您必须确保您的服务器正在使用mod_php运行PHP.

我正在nginx上运行Drupal并警告我

您的服务器无法显示文件上传进度.文件上传进度要求运行PHP且运行mod_php的Apache服务器."

I am having trouble getting the progress of a file upload using PHP, the server that my site is hosted on is using php v5.6.

I also have the 'uploadprogress' setting enabled in my cpanel manager.

I tried to follow this tutorial: Tracking Upload Progress with PHP and JavaScript

But I merged it with some other code that handles uploading of files using an ajax request.

My form code:

<form id="myForm"> <input type="hidden" value="myForm" name="<?php echo ini_get("session.upload_progress.name"); ?>"> <input type="file" id="file" /> <a href="javascript:uploadFile()">Upload</a> </form> Upload: <div id="process">0%</div>

Javascript code: var timer;

function uploadFile() { var uploadFile = $("#file")[0].files[0]; timer = setInterval(getStatus,1000); $.ajax( { url : "upload.php", type: 'POST', data : new FormData($('form')[0]), cache: false, contentType: false, processData: false, xhr: function() { var myXhr = $.ajaxSettings.xhr(); if (myXhr.upload) { // For handling the progress of the upload myXhr.upload.addEventListener('progress', function(e) { if (e.lengthComputable) { if (e.loaded < e.total) { $('#process').html(((e.loaded / e.total) * 100).toFixed(1) + "%"); } else { $('#process').html("Processing upload, please wait."); } } } , false); } return myXhr; }, success: function (response) { $('#process').html(response); clearTimeout(timer); } } ); } function getStatus() { $.get("progress.php", function(response) { $("#process2").html(response); } ); }

upload.php

session_start(); $uploadedFile = $_FILES["file"]; move_uploaded_file($uploadedFile['tmp_name'],$uploadedFile["name"]); echo $uploadedFile["name"]." was uploaded";

progress.php

session_start(); $key = ini_get("session.upload_progress.prefix") . "myForm"; if (!empty($_SESSION[$key])) { $current = $_SESSION[$key]["bytes_processed"]; $total = $_SESSION[$key]["content_length"]; echo $current < $total ? ceil($current / $total * 100) : 100; } else { echo 100; }

Unfortunately in my JS the line: $('#process').html(((e.loaded / e.total) * 100).toFixed(1) + "%");

only gets the time it takes to get the request, not actually upload the file.

I hope I have provided enough information to get some help.

解决方案

You have to be sure your server is running PHP with mod_php.

I'm running Drupal on nginx and warns me that

"Your server is not capable of displaying file upload progress. File upload progress requires an Apache server running PHP with mod_php."

更多推荐

使用PHP获取上传进度

本文发布于:2023-11-26 04:32:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度   上传   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!