想做一个上传文件的进度条,求大神看~ - ThinkPHP框架 https://www.thinkphp.cn/topic/8630.html
这是不用thinkphp框架写的
index.php文件
- <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
- <form id="upload-form"
- action="upload.php" method="POST" enctype="multipart/form-data"
- style="margin:15px 0" target="hidden_iframe">
- <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="test" />
- <p><input type="file" name="file1" /></p>
- <p><input type="submit" value="Upload" /></p>
- </form>
- <iframe id="hidden_iframe" name="hidden_iframe" src="about:blank" style="display:none;"></iframe>
- <div id="progress" class="progress" style="margin-bottom:15px;display:none;">
- <div class="bar" style="width:0%;"></div>
- <div class="label">0%</div>
- </div>
- <script>
- function fetch_progress(){
- $.get('progress.php',{ '<?php echo ini_get("session.upload_progress.name"); ?>' : 'test'}, function(data){
- var progress = parseInt(data);
- $('#progress .label').html(progress + '%');
- $('#progress .bar').css('width', progress + '%');
- if(progress < 100){
- setTimeout('fetch_progress()', 100);
- }else{
- $('#progress .label').html('完成!');
- }
- }, 'html');
- }
- $('#upload-form').submit(function(){
- $('#progress').show();
- setTimeout('fetch_progress()', 100);
- });
- </script>
- <?php
- if(is_uploaded_file($_FILES['file1']['tmp_name'])){
- move_uploaded_file($_FILES['file1']['tmp_name'], "./{$_FILES['file1']['name']}");
- }
- ?>
- <?php
- session_start();
- $i = ini_get('session.upload_progress.name');
- $key = ini_get("session.upload_progress.prefix") . $_GET[$i];
- 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;
- }
- ?>
php.ini中的配置也是修改好的。
然后是在thinkphp中配置 action是这样的
- <?php
- // 本类由系统自动生成,仅供测试用途
- class AddnewsAction extends Action {
- public function index(){
- $this->display();
- }
- public function uploadvideo(){
- if(is_uploaded_file($_FILES['file1']['tmp_name']))
- {
- echo ("$file is uploaded via HTTP POST");
- }
- else
- {
- echo ("$file is not uploaded via HTTP POST");
- }
- if(is_uploaded_file($_FILES['file1']['tmp_name'])){
- move_uploaded_file($_FILES['file1']['tmp_name'], "./{$_FILES['file1']['name']}");
- }
- }
- public function progress(){
- session_start();
- $i = ini_get('session.upload_progress.name');
- $key = ini_get("session.upload_progress.prefix") . $_GET[$i];
- 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;
- }
- }
- }
但是功能却不能实现了
通过progress()返回的数据直接是100;
通过测试发现那个是session中的值是空的;
不知道为什么。是因为thinkphp的原因?
我是通过
标签:uploaded,文件,name,进度条,upload,session,key,progress,上传 From: https://www.cnblogs.com/jmbt/p/16633574.html