title: Handling upload file with file_get_contents()
body: <p>I have a PHP script that handles uploaded CSVs into a temp directory, I then have 5 lines of code to convert from CSV to JSON.</p>
  
  <p><strong>My PHP Script:</strong></p>
  
  <pre><code>if (isset($_FILES['csvList']['type'])) {
  
    $validExtensions = array("csv");
    $temporary = explode(".", $_FILES["csvList"]["name"]);
    $file_extension = end($temporary);
  
    if (in_array($file_extension, $validExtensions)) {
  
      if ($_FILES["csvList"]["error"] &gt; 0) {
  
        echo "Return Code: " . $_FILES["csvList"]["error"] . "&lt;br/&gt;&lt;br/&gt;";
      } else {
  
        if (file_exists("/var/www/tmp/" . $_FILES["csvList"]["name"])) {
  
          echo $_FILES["csvList"]["name"] . " &lt;span id='invalid'&gt;&lt;b&gt;already exists.&lt;/b&gt;&lt;/span&gt; ";
  
        } else {
  
          $sourcePath = $_FILES['csvList']['tmp_name']; // Storing source path of the file in a variable
          $targetPath = "/var/www/tmp/".$_FILES['csvList']['name']; // Target path where file is to be stored
  
          move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
  
          $csvFile = $sourcePath;
          $csv = file_get_contents($csvFile);
          $csvArray = array_map("str_getcsv", explode("\n", $csvFile));
          $csvToJson = json_encode($csvArray);
          print_r($csvToJson);
        }
      }
    }
  }
  $sourcePath = $_FILES['csvList']['tmp_name'];       // Storing source path of the file in a variable
  $targetPath = "/var/www/tmp/".$_FILES['csvList']['name']; // Target path where file is to be stored
  move_uploaded_file($sourcePath,$targetPath) ;    // Moving Uploaded file
  </code></pre>
  
  <p><strong>The problem is on this line:</strong> <code>print_r($csvToJson);</code>. <strong>and this is the output:</strong></p>
  
  <pre><code>[["\/tmp\/phpYeuuBB"]]
  </code></pre>
  
  <p>This is the file path of my temporary file, what am I doing wrong?</p>
  
  <p><strong>Here's what my CSV looks like -</strong></p>
  
  <p><a href="https://docs.google.com/spreadsheets/d/1Ny9hyPtGrifh5OFWFbiQD-yFpkG3RbnFvoG_YQwwVPA/edit?usp=sharing" rel="nofollow noreferrer">CSV Demo</a></p>
  
  <p><strong>Update:</strong> My JSON is not formatted correctly with " and \ next to names</p>
  
  <pre><code>{"data":["[[\"Debra Brown\"],[\"Jacqueline Garza\"],[\"Kenneth Foster\"],[\"Antonio Howell\"],[\"Fred Rogers\"],[\"Robert Stanley\"],[\"Jesse Price\"],[\"Henry Bishop\"],[\"Marilyn Phillips\"],[\"Charles White\"],[\"Dennis Lawrence\"],[\"Nicholas Thompson\"],[\"Chris Graham\"],[\"Louis Dean\"],[\"Katherine Green\"],[\"Janice Peters\"],[\"Bobby Wood\"],[\"Bruce King\"],[\"Diane Mills\"],[\"Jane Fields\"],[\"Amanda Gutierrez\"],[\"Russell Cunningham\"],[\"Judith Matthews\"],[\"Carol Franklin\"],[\"Jose Murray\"],[\"Kathryn Cole\"],[\"Katherine Gardner\"],[\"Lois Woods\"],[\"Andrew Bryant\"],[\"Victor Wright\"],[\"Adam Russell\"],[\"Tina Gilbert\"],[\"Shawn Boyd\"],[\"Wanda Porter\"],[\"Rose Morris\"],[\"John Mccoy\"],[\"Frances Gibson\"],[\"Willie Lopez\"],[\"Chris Reyes\"],[\"Craig Vasquez\"],[\"Diane Simmons\"],[\"Mary Little\"],[\"Patricia Fowler\"],[\"Jane Perkins\"],[\"Juan Brooks\"],[\"Bruce Howard\"],[\"Tammy Richardson\"],[\"Jane Gomez\"],[\"Tammy Matthews\"],[\"Matthew Fox\"],[null]]"]}
  </code></pre>
  
  <p>How it should be - </p>
  
  <pre><code>{"data":[["Debra Brown"]]}
  </code></pre>
  
  <p><strong>When I print <code>$csv</code></strong></p>
  
  <p><a href="https://i.stack.imgur.com/p4e3e.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p4e3e.png" alt="enter image description here"></a></p>