search
HomeBackend DevelopmentPHP TutorialBeginners must read PHP file upload progress comprehensively revealed_PHP tutorial
Beginners must read PHP file upload progress comprehensively revealed_PHP tutorialJul 15, 2016 pm 01:27 PM
phpone timeuploadcomprehensiveRevealdocumentnewbieCompareuseResearchstillschedule

PHP is still relatively commonly used, so I studied the progress of PHP uploading files and shared it with you here. I hope it will be useful to everyone. The reason why it is difficult to implement an upload progress bar in PHP is because when we upload files to the server, we have to wait until all files are sent to the server before executing the corresponding PHP file. Before this, the file data was saved in a temporary file, and PHP could not obtain the path and size of this file.

Starting from Actionscript 2.0, Flash supports file upload and download. Although we cannot get the file upload progress on the server side, we can get the file sending progress on the server side. Based on this principle, you can use Flash to create an upload progress bar effect. I have seen some information on the Internet, but I feel it is flawed. So I did some research myself, strengthened the security and robustness of the program on the basis of predecessors, and added some customizable parameters. At present, there are two methods that I know of. One is to use the APC extension module written by Rasmus Lerdorf, the founder of PHP. The other method is to use the PECL extension module uploadprogress to implement it. Here are two examples of their respective implementations for reference. More Flexible application can be modified according to your needs.

APC implementation method of PHP file upload progress:

Install APC and refer to the official documentation for installation. You can use the PECL module installation method to quickly and easily configure PHP. There is no description here. ini, set the parameter apc.rfc1867=1 to enable APC to support the upload progress bar function. There are code examples in the APC source code description document:

<ol class="dp-xml">
<li class="alt"><span><span>if($_SERVER['REQUEST_METHOD']=='POST'){//上传请求  </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">status</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">apc_fetch</font></span><span>('upload_'.$_POST['APC_UPLOAD_PROGRESS']);  </span>
</li>
<li class="alt"><span>$status['done']=1;  </span></li>
<li class=""><span>echojson_encode($status);//输出给用户端页面里的ajax调用,相关文档请自己寻找  </span></li>
<li class="alt"><span>exit;  </span></li>
<li class=""><span>}elseif(isset($_GET['progress_key'])){//读取上传进度  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">status</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">apc_fetch</font></span><span>('upload_'.$_GET['progress_key']);  </span>
</li>
<li class=""><span>echojson_encode($status);  </span></li>
<li class="alt"><span>exit;  </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt"><span>//其他代码,比如上传表单等  </span></li>
<li class=""><span>} </span></li>
</ol>

PHP upload progress module implementation method for file upload progress :

Use the PECL module installation method to install the module. Set uploadprogress.file.filename_template="/tmp/upd_%s.txt" in php.ini. Code example:

<ol class="dp-xml">
<li class="alt"><span><span>if($_SERVER['REQUEST_METHOD']=='POST'){  </span></span></li>
<li class=""><span>if(is_uploaded_file($_FILES['upfile']['tmp_name'])){  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">upload_dir</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'your_path/'</font></span><span>;  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">ext</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">strrchr</font></span><span>($_FILES['video']['name'],'.');  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">sessid</font></span><span>=$_POST['UPLOAD_IDENTIFIER'];  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">tmpfile</font></span><span>=$upload_dir.$sessid;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">sessfile</font></span><span>=$upload_dir.$sessid.$ext;  </span>
</li>
<li class=""><span>if(move_uploaded_file($_FILES['upfile']['tmp_name'],$tmpfile)){  </span></li>
<li class="alt"><span>//上传成功  </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt"><span>//上传失败  </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt"><span>//上传错误  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>}elseif(!empty($_GET['sessid'])){  </span></li>
<li class=""><span>header("Expires:Mon,26Jul199705:00:00GMT");  </span></li>
<li class="alt"><span>header("Last-Modified:".gmdate("D,dMYH:i:s")."GMT");  </span></li>
<li class=""><span>header("Cache-Control:no-store,no-cache,must-revalidate");  </span></li>
<li class="alt">
<span>header("</span><span class="attribute"><font color="#ff0000">Cache-Control:post-check</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>,</span><span class="attribute"><font color="#ff0000">pre-check</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>",false);  </span>
</li>
<li class=""><span>header("Pragma:no-cache");  </span></li>
<li class="alt">
<span>header("Content-Type:text/html;</span><span class="attribute"><font color="#ff0000">charset</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">UTF</font></span><span>-8");  </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">unique_id</font></span><span>=$_GET['sessid'];  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">uploadvalues</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">uploadprogress_get_info</font></span><span>($unique_id);  </span>
</li>
<li class="alt"><span> </span></li>
<li class=""><span>if(is_array($uploadvalues)){  </span></li>
<li class="alt"><span>echojson_encode($uploadvalues);  </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt"><span>//读取进度失败,另外处理逻辑  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt"><span>//显示上传表单  </span></li>
<li class=""><span>}  </span></li>
</ol>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446544.htmlTechArticlePHP is still relatively commonly used, so I studied the progress of uploading files in PHP and shared it with you here. , hope it is useful to everyone. The reason why it is difficult to implement upload progress bar in PHP is...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
玩转雾锁王国的秘籍给新手指引玩转雾锁王国的秘籍给新手指引Jan 28, 2024 pm 03:33 PM

雾锁王国是一款开放世界的游戏,玩家可以扮演火焰之子进行生存和探索。游戏融合了动作RPG挑战的特色娱乐,为玩家带来无尽的惊喜和欢乐。在游戏中,玩家可以探索资源、环境和武器等内容。对于一些新手玩家,可能会对如何上手游戏感到好奇。在这期的介绍和分享中,我们将为大家提供一些相关的入门指南。雾锁王国新手入门技巧被瘴气笼罩区域的危险等级不同在探索的过程中会逐渐解锁地图的新区域,并且能看到被瘴气笼罩区域的位置。地图上会以两种颜色来区分,蓝色区域是可以短时间进入的,根据角色能力等级高低,可停留的时间也会有所区别

php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

锚点降临新手十连角色推荐锚点降临新手十连角色推荐Feb 20, 2024 pm 02:30 PM

锚点降临是一款拥有高画质美少女二次元主题的3D回合卡牌游戏,提供了丰富角色的精彩组合可供玩家来探索和体验,拥有很多精品阵容的强力搭配,新人玩家也是好奇新手池有哪些强力角色推荐,下面来看看新手十连金的选择参考!锚点降临新手池强力角色推荐第一个十连选艾莉丝,她主要是一个单体雷系爆发角色,单体输出非常爆炸的,对新人的体验也会是非常的友好的,所以十分推荐选取。10抽必金建议选择“艾莉丝”+“羚角”的组合,艾莉丝是最值得选的金皮雷属性输出角色,比新手卡池中的另外2个角色要强得不是一星半点。艾莉丝可以通过特

学会利用批量缩进是PyCharm新手必须掌握的技能学会利用批量缩进是PyCharm新手必须掌握的技能Dec 30, 2023 pm 12:58 PM

PyCharm新手必备技能:掌握批量缩进的使用方法,需要具体代码示例概述:PyCharm是一款功能强大的Python集成开发环境(IDE),它提供了许多实用的工具和功能,帮助开发者提高效率。在日常的编码过程中,我们经常需要对代码进行缩进,保持代码的格式整齐美观。而PyCharm提供的批量缩进功能可以帮助我们快速对代码进行批量缩进,提高编码效率。本文将探讨Py

上古王冠新手攻略玩法介绍上古王冠新手攻略玩法介绍Feb 20, 2024 am 11:20 AM

上古王冠是一款以西方魔幻冒险为背景,高品质有策略的卡牌手游。游戏内秘境探险、遗迹大冒险、全民冠军赛等特色玩法等你来体验。那么对于新手玩家来说,想要快速上手这个游戏当然少不了新手攻略了,今天小编就给大家带来其相关的攻略,一起看看吧。上古王冠新手攻略玩法一览一、开区流派:1.攒钻流:一切以攒钻为主,出村后再发力。除了主要的神兵、三个飞轮活动等需要花钻,其他一概不管,主打一个摆烂,英雄挑战不必理会,能打多少就多少,不强求。优点:只需摆烂攒钻,出村后能有钻石快速接入最新活动,拿新系列硬件英雄,钻石断档概

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

C语言和C++:哪个更适合编程新手C语言和C++:哪个更适合编程新手Mar 19, 2024 am 08:30 AM

C语言和C++:哪个更适合编程新手在现代科技高速发展的时代,学习编程已经成为一个越来越受欢迎的选择,无论是作为职业发展的一部分,还是作为提高逻辑思维能力的方式。而在众多编程语言中,C语言和C++都是非常经典且具有代表性的语言,许多人对于如何选择C语言还是C++来作为入门编程语言存在疑惑。那么,究竟是C语言更适合编程新手,还是C++更适合呢?需要具体代码示例来

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools