I have a frontend form(ajax submission) with a multi browse field. How can we read the browsed file in php and save the file to a folder specified.
I have refered this link. But not getting the browsed file data in pp function How to upload multiple files using PHP, jQuery and AJAX
Please see the script:
var formData = new FormData(jQuery(this).parents('form')[0]);
jQuery.ajax({
url: "<?php echo Mage::getUrl('a/b/c'); ?>",
type: 'POST',
xhr: function() {
var myXhr = jQuery.ajaxSettings.xhr();
return myXhr;
},
success: function (data) {
alert("Data Uploaded: "+data);
},
data: formData,
cache: false,
contentType: false,
processData: false
});
This is my php function:
public function fileuploadAction() {
Mage::log('ghj'.count($_POST['file']['name']), null, '107171.log');
Mage::log($this->getRequest()->getPost(), null, '107172.log');
for($i=0; $i<count($_FILES['file']['name']); $i++){
$target_path = "uploads/";
$ext = explode('.', basename( $_FILES['file']['name'][$i]));
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1];
if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
echo "The file has been uploaded successfully <br />";
} else{
echo "There was an error uploading the file, please try again! <br />";
}
}
The function is not entering to the loop. Also count($_POST['file']['name']) logs 0.
How can read this form data in function? Please help. Thanks in advance.