title: PHP upload with JSON return to jQuery (global) variable
body: <p>I need to take a CSV file from the client's machine and extract the data from it. I cannot save this CSV file, which is crucial (otherwise I would be laughing!). My PHP so far works like a charm:</p>
  <h2>upload.php</h2>
  <pre><code>&lt;?php
      $file = $_FILES['file'];
      if ($file['error'] === UPLOAD_ERR_OK) {
          $ext = substr($file[&quot;name&quot;], strrpos($file[&quot;name&quot;], '.') + 1);
          $maxSize = 3000000;
  
          //Check file extension, MIME-type and size limit (3 MB).
          if ($ext == &quot;csv&quot;) {
              if ($file[&quot;type&quot;] == &quot;text/csv&quot; ||
                  $file[&quot;type&quot;] == &quot;text/comma-separated-values&quot;) {
  
                  if ($file[&quot;size&quot;] &lt; $maxSize) {
                      //CSV -&gt; JSON
                      $fileAsArray = Array();
  
                      if (($handle = fopen($file[&quot;tmp_name&quot;], &quot;r&quot;)) !== FALSE) {
                          while (($data = fgetcsv($handle, 1000, &quot;,&quot;)) !== FALSE) {
                              $fileAsArray[] = $data;
                              }
                                  fclose($handle);
                              }
                              echo json_encode($fileAsArray);
                              die(json_encode($fileAsArray));
                      }
                      else {
                          echo &quot;File size: &quot;.$file[&quot;size&quot;].&quot;bytes&lt;br&gt;Limit: &quot;.$maxSize.&quot; (3MB)&quot;;
                      }
                  }
                  else {
                      echo &quot;MIME-type: &quot;.$file[&quot;type&quot;].&quot;&lt;br&gt;Required: text/csv&quot;;
                  }
              }
              else {
                  echo &quot;File Extension: &quot;.$ext.&quot;&lt;br&gt;Required: csv&quot;;
              }
          }
          else
              die(&quot;Cannot upload&quot;);
  ?&gt;
  </code></pre>
  <p>Ignore the echo. It was just so I knew it was actually working. However, I've been using jQuery with this website and can't figure out the code to properly get the form to submit the file and retrieve the JSON data back (fileAsArray) at the same time. Here is my HTML/jquery in <code>index.html</code>:</p>
  <pre><code>&lt;script type=&quot;text/javascript&quot;&gt;
      var csvData = {}; //My global variable (two-dimensional JSON array)
  
      $(document).ready(function(){
          $(&quot;#upload&quot;).click(function(){
              alert('test');
  
              $.ajax({
                  type: &quot;POST&quot;,
                  url: &quot;upload.php&quot;,
                  data: &quot;don't know what goes here&quot;,
                  dataType: &quot;json&quot;,
                  success: function (data) {
                      alert(data); //Tried everything here too. :(
                  }
              });
          });
      });
  </code></pre>
  <p>and HTML:</p>
  <pre><code>&lt;form method=&quot;POST&quot; action=&quot;upload.php&quot; enctype=&quot;multipart/form-data&quot; class=&quot;file_upload&quot;&gt;
      &lt;input id=&quot;getFile&quot; type=&quot;file&quot; name=&quot;file&quot;&gt;
      &lt;input value=&quot;Extract Data&quot; id=&quot;upload&quot; type=&quot;button&quot;&gt;
  &lt;/form&gt;
  </code></pre>
  <p>I tried <code>type=&quot;submit&quot;</code> as well, and a bunch of other things including just a 'form' tag and letting jQuery handle the POST... Thanks. If there is a better way to do this, please let me know :)</p>
  <p>(I've been struggling with this for several days now... Having gone through many renditions of the code outlined below, I've stripped it down to its basics.)</p>