Entering input file in jQuery FormData to send to PHP

I know I already got a question on this issue, but I still canโ€™t do it right. Need help.

I need to upload a file with additional data.

my input.php:

<input type="file" id="foto_path" name="foto_path" /> <input type="button" value="Add" onclick="javascript:sendForm()" /> 

I am posting this using javascript:

 function sendForm() { var fileInput = document.querySelector('#foto_path'); var oMyForm = new FormData(); var nip=123223374;//it will be generated by php, for temporary i just hardcode it oMyForm.append("foto_path", fileInput); oMyForm.append("nip",nip ); var oReq = new XMLHttpRequest(); oReq.open("POST", "upload-file.php", true); oReq.onload = function(oEvent) { if (oReq.status == 200) { //oOutput.innerHTML = "Uploaded!"; alert('success'); } else { //oOutput.innerHTML = "Error " + oReq.status + " occurred uploading your file.<br \/>"; alert('failed'); } }; oReq.send(oMyForm); } 

, and when I send this to the upload-file.php file:

 logapp("post -> ".print_r($_POST,true));//logapp is just function to log to file logapp("files -> ".print_r($_FILES,true)); 

I got this from the log:

 09/07/2013 02:47:06 pm :: post -> Array ( [foto_path] => [object HTMLImageElement] [nip] => 123223374 ) 09/07/2013 02:47:06 pm :: files -> Array ( ) 

I got a successful warning, but I need to get the files for foto_path as $_FILES not $_POST . My question is: why the input files are detected as $_POST["foto_path"] not as $_FILES["foto_path"] . How to change this to $_FILES["foto_path"] so that I can start working with the downloaded files?

+6
source share
1 answer

update: I solved this problem finally

 var ft=$('#foto_path_upload')[0].files[0]; oMyForm.append("foto_path_upload", ft); 

and I got this from the magazine:

 09/07/2013 04:26:53 pm :: files -> Array ( [foto_path_upload] => Array ( [name] => noimages.jpg [type] => image/jpeg [tmp_name] => D:\xampp\tmp\php4E90.tmp [error] => 0 [size] => 3642 ) ) 

The problem is resolved. thanks everyone

+5
source

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


All Articles