2

I have this code:

Html:

<input type="file" name="filename" id="filename">

jQuery:

$("#filename").change(function(e) {
var url = $("input#filename").val();

 $.ajax({            
        type: "POST",
        url: "/importArticleCsv",
        dataType: "json",
        data: "url="+ url,
        success: function(response)
        {
            alert("succes");

        }
    });
    return false;

});

php:

public function importArticleCsv(){      
    $url = $this->input->post('url');

        $csv_file = CSV_PATH . $url; // Name of your CSV file

$csvfile = fopen($csv_file, 'r');

$theData = fgets($csvfile);

$i = 0;

while (!feof($csvfile)) {
    $csv_data[] = fgets($csvfile, 1024);
    $csv_array = explode(",", $csv_data[$i]);

    $insert_csv = array();
    $insert_csv['ID'] = $csv_array[0];
    $insert_csv['name'] = $csv_array[1];
    $insert_csv['email'] = $csv_array[2];

$i++;

}

fclose($csvfile);

}

How can i upload the csv file, en send it to php so php can add everything in a array? I have all this code, and it goes to the right function but than it does nothing? how is this possible?

da1lbi3
  • 4,369
  • 6
  • 31
  • 65

1 Answers1

0

You can't use the .val() from a file upload directly (for good privacy/security reasons, e.g. otherwise website would be able to traverse your entire computer).

Try letting the script make the form do a post request (effectively sending the file to the desired PHP file for processing).

Or have a look at: How can i upload a file using jquery's $.ajax function with json and php

Community
  • 1
  • 1
Philip
  • 2,888
  • 2
  • 24
  • 36