Question

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

Was it helpful?

Solution

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

OTHER TIPS

I used qqFileUploader before. I checked my codes and here is how I accessed the file. You need to use $uploader ->file->getName().

$allowedExtensions = array('jpg','gif','png');
$sizeLimit = 2 * 1024 * 1024; //2mb
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$tmp = $uploader->file->getName(); // this is your $_FILES["qqfile"]
$result = $uploader->handleUpload('/path/where/to/upload/','filename.jpg');
if ($result['success']) {
    // successfully moved the file from temp location
} else {
    // not, error
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top