Ajax multiple download doenst php file processing

Hi, I am using fineupload to upload multiple script files , but I can not understand. I am trying to do a file processing third-party server php script.

when u turns on

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

html to show what 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 check and stuff, but I can’t get the file information

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 an error because the file is not installed as an error in the download form. So it gets the error from the php file ... but what does it send? how can i find it

also when sending a success message

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

the download form says the file is uploaded in green.

+2
source share
2 answers

He does not know if you have already found a solution, but maybe you can take a look at this:

first html form

just create a regular html form without a submit button, but just a button. note that my solution also has a fantastic download 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 like that to select only the file types you need.

then loading using jquery / javascript

Sounds like you, 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 left it easy. However, you can make a javascript function to also check files and, if you want, the whole form. just return the function name for beforeSend: youvalfunctname , you can also create a callback there like beforeSend: function(){ //do stuf here } . And if loading errors occur, you can do the same with error:

Server php. Finally

You can take the form here however you want, but I will just give an example of how you could do this.

 <?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 watch a demo specially created for uploading images: Note is not always online

Demo code example is also provided.

+8
source

I have used qqFileUploader before. I checked my codes and this 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 } 
0
source

Source: https://habr.com/ru/post/1482641/


All Articles