-3

I have some problems here:

I have index.html, that contains upload file feature

I have cleaning.php, it will cleaning every string in a file

So, what I want is if I run index.html then upload a file, at that time it will execute cleaning.php and return the clean file to index.html How can I do that? Need your help :(

This is my index.html file:

 <script>
 $(function testing (data) {

    $("#upload").bind("click", function () {
       // I want to pass this array to cleaning.php
       $someArray[] = "stringFromCSVFile"; // must pass to cleaning.php
  });
 </script>

This is my cleaning.php file:

$array = csvToArray("filecsv.csv"); //convert uploaded file to array
callFuncA($array);

functionA($array){
   return $something;
}
jill182
  • 91
  • 1
  • 3
  • 9

2 Answers2

0

You can try to use ajax post for files this way and you can do changes in cleaning.php as your requirements and return your response to index.html.

index.html file:

<form id="data" method="post" enctype="multipart/form-data">
    <input type="file" />
    <button type="submit">Submit</button>
    <div id="response"></div>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script>
var files;
$('input[type=file]').on('change', prepareUpload);
function prepareUpload(event){
  files = event.target.files;
}
$('form').on('submit', uploadFiles);
function uploadFiles(event){
    event.stopPropagation(); 
    event.preventDefault();
    var data = new FormData();
    $.each(files, function(key, value){
        data.append(key, value);
    });
    $.ajax({
        url: 'cleaning.php',
        type: 'POST',
        data: data,
        cache: false,
        dataType: 'json',
        processData: false,
        contentType: false,
        success: function(data, textStatus, jqXHR){
            //Successfuly Uploaded
            $('#response').html(JSON.stringify(data));
        },
        error: function(jqXHR, textStatus, errorThrown){
            $('#response').html('ERRORS: ' + textStatus);
        }
    });
}
</script>

cleaning.php file:

<?php
echo json_encode($_FILES);
?>
Rahul K
  • 413
  • 3
  • 11
  • Thank you :) but when I test to run the code, it doesn't give me an output :( – jill182 Aug 04 '16 at 08:08
  • You can easily run this code and you can clean file after upload in cleaner.php. – Rahul K Aug 04 '16 at 09:55
  • actually my limitation: file that can be upload are just .csv file. And I already store those csv string in `someArray[]` in javascript and ready to pass into php. Is there another solution for my case? – jill182 Aug 04 '16 at 10:01
  • Its better, you can pass file to cleaner.php and generate array using php and return back your response as you needs. – Rahul K Aug 04 '16 at 10:09
-1

It's impossible, ajax cannot read the contents of uploaded files.

You should put an upload form in index.html and submit it to cleaning.php which does cleaning in PHP and returns the result.

vincent163
  • 384
  • 2
  • 13