Parse CSV file to JSON after uploading using temporary copy

I’m almost finishing building a functionality for parsing CSV files to JSON and just need help in piecing it together. The way it works is that files will be uploaded using AJAX/Jquery. Once the files has been uploaded and sent the PHP to parse the CSV file to JSON will then execute.

After parsing, the converted file will be push or sent to the API as a JSON object. Here is the code that I have been working. For the uploading i’m using this plugin AJAX/JQuery File Uploader

This functionality is also built on using RactiveJS

AJAX/Jquery File Uploader

This is the section where I’m currently uploading or placing the file. URL is pointed at the upload.php.

<div id="fileuploader">Upload</div>

$(document).ready(function() {
		url: 'upload.php',
        autoSubmit: false,



Is there a way to GET the temporary copy of the uploaded file and parse using the PHP code I have built below to convert the CSV to JSON


    if ( 0 < $_FILES['file']['error'] ) {
        echo 'Error' . $_FILES['file']['error'] . '<br/>';

    else {

        move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);




Right now be the file is being declared manually.

    $json_data = csvToJson('lms.csv');


    function csvToJson($fname) {
        if (!($fp = fopen($fname, 'r') )) {
            die("Can't open file");

        else {
            ('Upload File');

        $key = fgetcsv($fp, "1024", ",");

        $json = array();
            while ($row = fgetcsv($fp, "1024", ",")) {
                $json[] = array_combine($key, $row);


        foreach ( $json as $k=>$v ) {

            $json[$k]['accountName']           =     $json[$k]['ACCOUNT NAME']; 
            $json[$k]['dateRequested']         =     $json[$k]['DATE']; 

            unset($json[$k]['ACCOUNT NAME']);





<?php // $target_file = fopen($_FILES["fileToUpload"]["tmp_name"], 'r'); ?>

Send Converted to API (Ractive/AJAX/JS)

As you can see the sending part is triggered by an on-click event (app.on)

<script src=""></script>

<script type="text/javascript">
var app = new Ractive({
    el       : '#container',
    template : '#template',
    data     : { name: 'world' }

    var proxy       = '';
    var endpoint    = 'account/leads/';
    var rt          = 'POST';
    var url         = proxy+'?endpoint='+endpoint+'&rt='+rt;

    var lms_json    = <?php echo json_encode($json_data); ?>;
    var jobjects    = JSON.parse(lms_json);

    for ( i = 0; i < jobjects.length; i++ ) {
        var data = jobjects[i];


              type     : 'POST',
              url      : url,
              data     : data,
              dataType : 'json',
              success  : function() {

              error    : function(error) {



This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.