0

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.

E.B
  • 131
  • 6
  • `console.log(formData)` should have been your first diagnostic step. – Xorifelse Jul 11 '17 at 15:49
  • Also your file upload script is *not* secure. I could easily submit a PHP script. Read my [documentation](https://stackoverflow.com/documentation/php/2781/security/29134/uploading-files#t=201707111552112081147) on how to properly upload files. – Xorifelse Jul 11 '17 at 15:52

0 Answers0