2014-10-30 61 views
0

所以我在互联网上搜索多上传的可能性,它显示上传时的进度条。由于那里有很多,我很快找到一个适合我的需求。PHP + jQuery上传包括我的网站再次在状态

现在我在上传完成后遇到问题。一旦上传完成,它应该显示上传“文件[编号]:文件名(大小)已被上传”DIV的状态。不过,它不仅显示我的状态,还包括我的网站布局,作为重复。

请能有人帮我这个,因为我坐在它一整天,但没有找到错误:(

这是HTML +形式:

<div id="main"> 
<form id="pupload" action="upload.php" method="POST" enctype="multipart/form-data"> 
    <fieldset class="tabulated"> 
     <table id="down" class="bbcode_table" cellspacing="1"> 
      <thead> 
       <tr class="Cnorm"> 
        <td><input type="file" name="files[]" multiple="multiple" id="files"></td> 
       </tr> 
      </thead> 
      <tbody> 
       <tr class="Cmite"> 
        <td><input id="submit" class="button1" type="submit" value="Upload"></td> 
       </tr> 
      </tbody> 
     </table> 
    </fieldset> 
</form> 
<div class="progress"> 
     <div class="bar"></div> 
     <div class="percent">0%</div> 
</div> 
{msg} 
<div id="status"></div> 

<script> 
    jQuery(document).ready(function ($) { 
    "use strict"; 

var bar = $('.bar'); 
var percent = $('.percent'); 
var status = $('#status'); 

$('form').ajaxForm({ 
beforeSend: function() { 
    status.empty(); 
    var percentVal = '0%'; 
    bar.width(percentVal); 
    percent.html(percentVal); 
}, 
uploadProgress: function(event, position, total, percentComplete) { 
    var percentVal = percentComplete + '%'; 
    bar.width(percentVal); 
    percent.html(percentVal); 
}, 
success: function(data, statusText, xhr) { 
    var percentVal = '100%'; 
    bar.width(percentVal); 
    percent.html(percentVal); 
    status.html(xhr.responseText); 
}, 
error: function(xhr, statusText, err) { 
    status.html(err || statusText); 
} 
}); 

}); 
</script> 
</div> 

所需的jQuery的文件。都被称为在网站标题

这是PHP代码吧:

<?php 

defined ('main') or die ('no direct access'); 

$main_dir = 'include/downs/public-upload/'; 

// Upload dirs sorted by file types 
$files = $main_dir.'files/'; 
$images = $main_dir.'images/'; 
$media = $main_dir.'media/'; 
$video = $main_dir.'video/'; 

// File extensions 
$files_ext = array('apk','exe','doc','docx','docm','gadget','html','ini','pdf','php','rar','sh','txt','xlsx','zip'); 
$images_ext = array('gif','jpg','JPG','jpe','jpeg','JPEG','png','PNG'); 
$media_ext = array('mp3','ogg','wav'); 
$video_ext = array('avi','mp4','3gp'); 

$msg = ''; 
     // Check rights first to make sure we can put the file in the directory 
     if (!is_writeable ($main_dir)) { 
       $msg = 'The folder "include/downs/<b>public-upload</b>/" requires WRITE (chmod 777) permission!!! Please set the WRITE (chmod 777) permission and reload the page.'; 
     } 
     // Now we can upload our file 
     $tpl = new tpl ('admin/pupload/pupload_upload'); 
      if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_FILES['files'])) 
      { 
       // loop all files 
       foreach ($_FILES['files']['name'] as $i => $name) 
       { 
        $pathinfo = pathinfo($name); 

        // if file not uploaded then skip it 
         if (!is_uploaded_file($_FILES['files']['tmp_name'][$i])) 
         continue; 

        // now we can move the uploaded files 
        // IMAGES 
        if (in_array($pathinfo['extension'], $images_ext)) { 

        if (move_uploaded_file($_FILES['files']['tmp_name'][$i], $images . $name)) 
        { 
         $msg .= '<b>File ' .($i+1). ':</b> ' . $name . '&nbsp; (' . niceBytes($_FILES['files']['size'][$i]) . ') <font color="#00FF00">successfully uploaded</font><br />'; 
        } else { 
         $msg .= '<b>File ' .($i+1). ':</b> ' . $name . '&nbsp; (' . niceBytes($_FILES['files']['size'][$i]) . ') <font color="#FF0000">not successfully uploaded</font><br />'; 
        } 
        // FILES 
        } else if (in_array($pathinfo['extension'], $files_ext)) { 

        if (move_uploaded_file($_FILES['files']['tmp_name'][$i], $files . $name)) 
        { 
         $msg .= '<b>File ' .($i+1). ':</b> ' . $name . '&nbsp; (' . niceBytes($_FILES['files']['size'][$i]) . ') <font color="#00FF00">successfully uploaded</font><br />'; 
        } else { 
         $msg .= '<b>File ' .($i+1). ':</b> ' . $name . '&nbsp; (' . niceBytes($_FILES['files']['size'][$i]) . ') <font color="#FF0000">not successfully uploaded</font><br />'; 
        } 
        // VIDEOS 
        } else if (in_array($pathinfo['extension'], $video_ext)) { 

        if (move_uploaded_file($_FILES['files']['tmp_name'][$i], $video . $name)) 
        { 
         $msg .= '<b>File ' .($i+1). ':</b> ' . $name . '&nbsp; (' . niceBytes($_FILES['files']['size'][$i]) . ') <font color="#00FF00">successfully uploaded</font><br />'; 
        } else { 
         $msg .= '<b>File ' .($i+1). ':</b> ' . $name . '&nbsp; (' . niceBytes($_FILES['files']['size'][$i]) . ') <font color="#FF0000">not successfully uploaded</font><br />'; 
        }     
        // MEDIA 
        } else if (in_array($pathinfo['extension'], $media_ext)) { 

        if (move_uploaded_file($_FILES['files']['tmp_name'][$i], $media . $name)) 
        { 
         $msg .= '<b>File ' .($i+1). ':</b> ' . $name . '&nbsp; (' . niceBytes($_FILES['files']['size'][$i]) . ') <font color="#00FF00">successfully uploaded</font><br />'; 
        } else { 
         $msg .= '<b>File ' .($i+1). ':</b> ' . $name . '&nbsp; (' . niceBytes($_FILES['files']['size'][$i]) . ') <font color="#FF0000">not successfully uploaded</font><br />'; 
        } 
        } else { 
         $msg .= '<b>File ' .($i+1). ':</b> ' . $name . '&nbsp; (' . niceBytes($_FILES['files']['size'][$i]) . ') <font color="#FF0000">has an unsupported ending</font><br />'; 
        } 
       } 
      } 
$tpl->set_ar_out(array('msg' => $msg), 0); 
?> 

上传正在将文件放入其文件夹中,并且还显示“上传成功”消息,但它再次包含我的网站布局,就好像我将运行“include()”功能一样。

我非常乐意为此提供任何帮助。

+0

也许清洗$('#状态')之前? status.html( “”); – 2014-10-30 18:11:13

+0

所以你的意思是把status.empty()放在status.html()前面的success:function?我尝试过,但不幸的是它没有改变任何东西:( – 2014-10-30 18:31:19

+0

也许xhr.responseText也会返回头文件在upload.php中检查头文件 – 2014-10-30 18:51:43

回答

0

此问题的解决方案可能是2:

1修改目标页面(upload.php的)只打印您需要的数据。

2。在你的Ajax功能的过滤器“xhr.responseText”要

我将决定自己的第一选择做变量味精的回声在upload.php的只显示数据。然后在成功ajax上,如果“status.html(xhr.responseText)”仍然检索整个页面,你可以尝试“status.html(data)”。

0

我想我找到了错误!它似乎是我的模板系统,它正在做奇怪的事情,因为头部的东西让我调整了一些东西,事后它工作得很好。我已经修好了,但不得不再次重新配置它,因为我再次玩它)感谢提示ADASein