PHP+jQuery上传再次包含我的网站状态

PHP+jQuery Upload includes my website again in status

本文关键字:我的 网站 状态 包含 PHP+jQuery      更新时间:2023-09-26

所以我在互联网上搜索了多次上传的可能性,它显示了上传时的进度条。由于有很多,我很快就找到了一个适合我需要的。

现在我在上传完成后遇到了一个问题。上传完成后,它会在DIV中显示上传的状态"File[number]:Filename(size)has uploaded"。然而,它不仅会显示状态,还会重复显示我的网站布局。

请有人帮我解决这个问题,因为我坐了一整天都找不到错误:(

这是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()"函数一样。

我很感激能在这件事上得到任何帮助。

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

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

2-在您的ajax函数过滤器"xhr.responseText"中,只显示您想要的数据

我会决定自己选择第一个选项,在upload.php中回声变量msg。然后在成功的ajax上,如果"status.html(xhr.responseText)"仍然检索整个页面,你可以尝试"status.html(data)"。

我想我发现了错误!这似乎是我的模板系统在做一些奇怪的事情,因为标题的事情让我调整了一些事情,后来它工作得很好。我把它修好了,但由于我又在玩它,我不得不重新安装;)感谢ADASein 的提示