duankange2433 2012-09-16 09:28
浏览 51
已采纳

ajax多个上传doenst php文件处理

Hi i'm using the fineupload multi file uploading script, but there is something i can not get a grip of. I'm trying to make a php server side file processing script.

when u include

<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();
    });
});

the html to show it is

    <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>

now in up.php i want to do the file validation and stuff but i can not get the file information

the code

<?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"));
}
?>

gives the error there is no file set as a error on the upload form. So it does recieves the error from the php file... but what does it send? how can i find it

also when I send the success message back

echo json_encode(array('success' => TRUE));

the upload form says file uploaded an turns green..

  • 写回答

2条回答 默认 最新

  • dongsanhu4784 2013-06-13 08:06
    关注

    He I don't know if you already found a solution but Maybe you can take a look at this:

    first html form

    just build a usual html form without submit button but just a button button. note that my solution also has a fancy loading bar!

    <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>
    

    now you can add accept="image/*" or something in that fashion to only select the file types you need or want.

    then the uploading with jquery/javascript

    Looks like yours but then better.

    $(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});
        }
    }
    

    believe me I kept this easy. You can however make a javascript function here to also validate the files and if you want the entire form. just put you validate function name behind beforeSend: youvalfunctname you can also create a call back there like beforeSend: function(){ //do stuf here }. And for when there do occur errors in uploading you can do the same with error:.

    The server side php. At last

    You can take it form here doe what you want but I just give an example how you could do it.

    <?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;
    
    ?>
    

    You can also take a look at a demo specially made for uploading images: Note not always online

    Also here is the code example of the demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大