AJAX form - uploading multiple images - php

I have an ajax call, to upload multiple images from javascript. The code works well when I have a few images but as soon as I increase the files uploaded at the same time (ex: 530 files), the php does not receive anything. Any help to solve this issue please?
$("form").on("submit", function(e) {
e.preventDefault();
var formdata = new FormData(this);
formdata.append('regId', document.getElementById('register_id').value);
$.ajax({
url: 'uploadImages.php',
type: 'post',
data: formdata,
contentType: false,
processData: false,
success: function(response) {
document.getElementById("body").innerHTML = "<p>"+response+"</p>";
},
});
});
I tried to increase the max_upload_size and max_file_uploads from apache2 as this example:
https://connectwww.com/how-to-change-php-file-upload-size-limit-in-ubuntu/5087/
But still it does not work.

Related

AJAX POST requests getting lost

I'm having a problem trying to implement an AJAX request into a pre-existing website that I didn't create.
I have no problems sending the data to PHP as a GET request, however POST requests and trying to access $_FILES is returning null.
Here is the AJAX:
var someData = 'test';
$.ajax({
url: "post-data.php",
method: "POST",
dataType: "JSON",
data: ({someData}),
success: function(data) {
console.log(data);
}
});
PHP:
<?php echo json_encode($_POST['someData']); ?>
I believe the cause of the issue might lie within the htaccess file or be related to some other redirect that is in place on the site. This website was built by a past staff member at the company, and I've had this same problem using AJAX POST with several other sites built by them.
As changing from POST to GET works fine I don't think there is any problem with my very simple code.
Is there some way I can test if the data is going missing due to a redirect, or could there be some other cause?
First check the browser dev tools ctr+shift+J and see if there is a redirect. If not then
You need to set your datatype to json and depending on what version of JQUERY you are using you might have to use "type" instead of "method" if your JQUERY version < 1.9.0
var someData = 'test';
$.ajax({
url: "post-data.php",
method: "POST",
dataType: "json",
data: ({someData}),
success: function(data) {
console.log(data);
}
});
PHP code:
header("Content-Type: application/json", true);
If that doesnt work then make sure your URL is absolutely correct. No extra slashes or spaces.
I have managed to figure this out, there is a 302 redirect in place for all .php extetensions to a url without the extension. There was also an error in my code.
Changing the URL in the AJAX to "post-data" without the .php extension allows me to see $_POST in PHP.
My other problem with not being able to access files came down to the way I was trying to send FormData with AJAX.
My original code to do this was:
var someData = 'test';
var fData = new FormData();
fData.append("images", $("input[name='fileUpload']").prop("files")[0]);
$.ajax({
url: "post-data.php",
method: "POST",
dataType: "JSON",
contentType: false,
processData: false,
data: ({someData, fData}),
success: function(data) {
console.log(data);
}
});
The problem being that you can't send FormData and other variables at the same time. Instead I have to append my other data to the FormData:
var someData = 'test';
var fData = new FormData();
fData.append("images", $("input[name='fileUpload']").prop("files")[0]);
fData.append("someData", someData);
$.ajax({
url: "post-data.php",
method: "POST",
dataType: "JSON",
contentType: false,
processData: false,
data: fData,
success: function(data) {
console.log(data);
}
});

Blob image url to php not working

This is my first time trying to deal with blob data.
I'm trying to send the blob image url via ajax to php for uploading the icon but it's not working, I am not sure if I'm even doing it correctly, can someone help me out?
console.log(apk.app.icon_url); // Prints: blob:http://localhost/9ca5a837-fdb4-4288-a21a-18e9650e6ac5
let data = new FormData();
data.append('icon', apk.app.icon_url);
$.ajax({
url: "testicon.php",
type: 'POST',
data: data,
contentType: false,
processData: false,
success: function(data) {
alert(data); // PHP array returns empty
}
});

append formData multiple files and other inputs - php processing

I have multiple forms generated dynamically.
I need to build the formData for multiple files as well as a couple of extra hidden inputs.
This is what I have tried so far:
$(document).on('click', '.upload-btn', function(e){
e.preventDefault();
var formData = new FormData($(this).parents('form')[0]);
$.ajax({
method: "POST",
url: "upload.php",
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: formData,
beforeSend: function(){
...
},
success: function(){
...
}
});
});
Oddly enough, this works somehow, and the files do get uploaded to the server, I can perform all sorts of operations on the temp files but...
the files are never a part of the $_POST array if I var_dump $_POST the files are not in there.
Is there a better way to build the formData? am I missing something on the php side when I'm testing for isset($_POST['file[]'])
Any help is greatly appreciated.
Use php's files array. print_r($_FILES);
Doing the above print, you'll be able to access every file sent from your form. This includes keys, tmp_name etc

Error in AjaX file upload: uploaded file is null

I'm trying to upload a file via Ajax.
var fd = new FormData();
fd.append('file', file);
var xhr = $.ajax({
url: 'https://www.mywebsite.com/it/file/upload/',
type: 'POST',
dataType: 'json',
data: fd,
cache: false,
contentType: "application/json; charset=utf-8"
processData: false,
success: function(result, message, xhr)
{
console.log(result);
}
});
For the moment, the upload PHP script simply displays file data
header('Content-Type: application/json');
echo json_encode($_FILES['file']);
die();
As stated here, I am forced to use contentType:"application/json; charset=utf-8" because contentType:false causes a 404 (Not Found) error.
Unfortunately, this solution avoids the 404 error, but the displayed file data is null.
You should use this. It is working for me.
$.ajax({
type: 'POST',
url: $(this).attr('action'), //url_to_php_script
data: new FormData(this),
contentType: false,
processData: false,
success: function (data) {
//do necessary cleanups on success
},
error: function (e) {
//do necessary cleanups on failue
}
});
You will be able to get uploaded image in PHP using $_FILES
It should be ok the way you're doing it, however...
If you set contentType to false, than you will force jquery not to set the content type for you. If it is not set, it will default to `application/x-www-form-urlencoded; charset=UTF-8'.
That means you are sending your data using an url encoded string.
You can't easily send a file in an url encoded string.
Most likely the method on the server side is set up to read the form parameters from the url. Therefor if you set `contentType' to false, the server won't recognise the url, resulting in a 404.
Try setting up the server method to accept post data, remove the url parameters and read the posted form from the $_POST and $_FILES variables.
If you can't figure it out, update the question with the servers' method so we can see how the method is set up.

How to solve Firebug’s “Aborted” messages when sending form data via AJAX?

I have created a PHP web application and use MySQL as database backend, but there is an 'Aborted' note on Firebug's Net panel's status column when I access the web page. Why?
$('#submit').on('click', function () {
// e.preventDefault();
var formData = JSON.stringify($("#frmPayoye").serializeObject());
console.log(formData);
$.ajax({
type: "POST",
url: "http://www.sanaroopay.com/pg/api/ectransact/",
data: formData,
cache: false,
timeout: 60000,
async: false,
processData: true,
dataType: 'json', //you may use jsonp for cross origin request
contentType: "application/json; charset=utf-8",
crossDomain: true,
success: function (data) {
alert(JSON.parse(data));
// alert("ok");
console.log("success");
// window.location.assign('https://secure.payoye.net/web/merchant');
},
error: function () {
console.log("Failed");
}
});
});
You are not cancelling the form submission so the Ajax call is aborted and the page submits the form as it is designed to do. So you need to stop the form submission.
$('#submit').on('click', function (evt) {
evt.preventDefault(); //stop the default action of the button
//Rest of your code
});
Please see the documentation of XHR open() for example here: https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest
Note: Calling this method an already active request (one for which open()or openRequest()has already been called) is the equivalent of calling abort().
Just create a new XHR instance whenever you need one. Better yet, use jQuery or other JS library to do AJAX. It should shield you from these intricacies.
How to solve Firebug’s “Aborted” messages upon Ajax requests?

Categories