ajax多个上传doenst php文件处理

r2g*_*get 2 php validation ajax jquery multifile-uploader

嗨,我正在使用fineupload多文件上传脚本,但有一些我无法掌握.我正在尝试制作一个php服务器端文件处理脚本.

当你包括

<script type="text/javascript">
$(document).ready(function() {
    var uploader = new qq.FileUploader({
        element: $('#manualUploadModeExample')[0],
        action: "core/up.php",
        autoUpload: false,
        demoMode: false,
        debug: false,
        multiple: true,
        maxConnections: 3,
        disableCancelForFormUploads: false,
        //0 is geen limit getal in bytes
        minSizeLimit: 0,
        sizeLimit: 0,
        inputName: "qqfile",
        uploadButtonText: "Select Files",
        cancelButtonText: "verwijder",
        failUploadText: "Upload mislukt"
    });

    $('#triggerUpload').click(function() {
        uploader.uploadStoredFiles();
    });
});
Run Code Online (Sandbox Code Playgroud)

显示它的HTML

    <div id="Upload">
    <noscript>
        <p>Please enable JavaScript to use file uploader.</p>
        <!-- or put a simple form for upload here -->
    </noscript>
    <ul id="manualUploadModeExample" class="unstyled"></ul>
    <span id="triggerUpload" class="btn btn-primary">Upload Queued Files</span>
</div>
Run Code Online (Sandbox Code Playgroud)

现在up.php我想做文件验证和东西,但我无法获取文件信息

代码

<?php
if(isset($_FILES["qqfile"])){
    echo json_encode(array('error' => "There is a file to work with"));
}
else
{
    echo json_encode(array('error' => "there is no file set"));
}
?>
Run Code Online (Sandbox Code Playgroud)

给出错误,没有文件设置为上传表单上的错误.所以它确实收到了来自php文件的错误......但是它发送了什么?我怎么能找到它

当我发回成功消息时

echo json_encode(array('success' => TRUE));
Run Code Online (Sandbox Code Playgroud)

上传表格说文件上传了一个绿色..

bot*_*wer 8

他我不知道你是否已经找到了解决办法,但也许你可以看看这个:

第一个HTML表单

只需构建一个通常的html表单,无需提交按钮,只需按一个按钮.请注意我的解决方案也有一个花哨的加载栏!

<form enctype="multipart/form-data" id="myform">    
    <input type="text" name="some_usual_form_data" />
    <br>
    <input type="text" name="some_other_usual_form_data" />
    <br>
    <input type="file" multiple name="file[]" id="image" /> <sub>note that you have to use [] behind the name or php wil only see one file</sub>
    <br>
    <input type="button" value="Upload files" class="upload" />
</form>
<progress value="0" max="100"></progress>
<hr>
<div id="content_here_please"></div>
Run Code Online (Sandbox Code Playgroud)

现在你可以添加accept="image/*"或以某种方式的东西只选择你需要或想要的文件类型.

然后用jquery/javascript上传

看起来像你的,但更好.

$(document).ready(function () { 
    $('body').on('click', '.upload', function(){
        // Get the form data. This serializes the entire form. pritty easy huh!
        var form = new FormData($('#myform')[0]);

        // Make the ajax call
        $.ajax({
            url: 'action.php',
            type: 'POST',
            xhr: function() {
                var myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){
                    myXhr.upload.addEventListener('progress',progress, false);
                }
                return myXhr;
            },
            //add beforesend handler to validate or something
            //beforeSend: functionname,
            success: function (res) {
                $('#content_here_please').html(res);
            },
            //add error handler for when a error occurs if you want!
            //error: errorfunction,
            data: form,
            // this is the important stuf you need to overide the usual post behavior
            cache: false,
            contentType: false,
            processData: false
        });
    });
}); 

// Yes outside of the .ready space becouse this is a function not an event listner!
function progress(e){
    if(e.lengthComputable){
        //this makes a nice fancy progress bar
        $('progress').attr({value:e.loaded,max:e.total});
    }
}
Run Code Online (Sandbox Code Playgroud)

相信我,我保持这一点.但是,你可以在这里制作一个javascript函数来验证文件,如果你想要整个表格.只需将验证函数名称放在后面,beforeSend: youvalfunctname您也可以在那里创建一个回调函数beforeSend: function(){ //do stuf here }.而且,如果在上传时出现错误,您也可以这样做error:.

服务器端php.最后

你可以在这里形成你想要的东西,但我只是举例说明你是如何做到的.

<?php

    $succeed = 0;
    $error = 0;
    $thegoodstuf = '';
    foreach($_FILES["file"]["error"] as $key => $value) {
        if ($value == UPLOAD_ERR_OK){
            $succeed++;

            //took this from: "https://stackoverflow.com/questions/7563658/php-check-file-extension"
            //you can loop through different file types
            $file_parts = pathinfo($filename);
            switch($file_parts['extension'])
            {
                case "jpg":

                    //do something with jpg

                break;

                case "exe":

                    // do sometinhg with exe

                break;

                case "": // Handle file extension for files ending in '.'
                case NULL: // Handle no file extension
                break;
            }

            $name = $_FILES['file']['name'][$key];

            // replace file to where you want
            copy($_FILES['file']['tmp_name'][$key], './upload/'.$name);

            $size = filesize($_FILES['file']['tmp_name'][$key]);
            // make some nice html to send back
            $thegoodstuf .= "
                                <br>
                                <hr>
                                <br>

                                <h2>File $succeed - $name</h2>
                                <br>
                                    give some specs:
                                    <br>
                                    size: $size bytes
            ";
        }
        else{
            $error++;
        }
    }

    echo 'Good lord vader '.$succeed.' files where uploaded with success!<br>';

    if($error){
        echo 'shameful display! '.$error.' files where not properly uploaded!<br>';
    }

    echo '<br>O jeah there was a field containing some usual form data: '. $_REQUEST['some_usual_form_data'];
    echo '<br>O jeah there was a field containing some usual form data: '. $_REQUEST['some_other_usual_form_data'];

    echo $thegoodstuf;

?>
Run Code Online (Sandbox Code Playgroud)

您还可以查看专门用于上传图像的演示:请注意并非总是在线

这里还有演示的代码示例