0

I am using React.js as a frontend and I'm trying to send a .csv file to my backend which is written in PHP.

PHP doesn't seem to be accepting my csv files. I have tested with other file type (PDF, PNG, JPG) and all of these turning up on the backend.

I have also tried to change Content-Type to multipart/form-data and text/csv on both axios and php backend.

<?php
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: GET, POST");
    header("Access-Control-Allow-Headers: Content-Disposition, Content-Type, Content-Length, Accept-Encoding");
    header("Content-type: multipart/form-data");

    require_once 'jwt_utils.php';

    $json_params = file_get_contents("php://input");

   if (strlen($json_params) > 0) {
    $decoded_params = json_decode($json_params);
   }
   $output = "No request recognised";

   if (json_last_error() != JSON_ERROR_NONE) {
    echo ($output);
    exit(0);
   }

   // if($_FILES){
   //  $file = $_FILES["data"];
   // }

   echo json_encode(["status"=>$status, "json"=>$decoded_params, "_File"=>$_FILES]);


?>

The above PHP doesn't even return a a json encoded string. It returns HTML like:

<!DOCTYPE html>\r\n\r\n<html lang=\"en\">\r\n  \ .....

uploadEcg.js

export const uploadECG = async (formData, jobName, jobID) => {
  const response = await axios
    .post("upload_ecg.php", formData, {
      headers: { "Content-Type": "multipart/formdata" },
    })
    .then((res) => console.log(res));
};

submitJob

const submitJob = async (e) => {
var formData = new FormData();
    e.preventDefault();
    // getFileContext();

    formData.append("file", file);
    console.log([...formData]);
    const response = await uploadECG(formData, "Test Name", "Test ID");
  };
Harry
  • 95
  • 1
  • 10
  • 1
    Does this answer your question? [How to extract data from csv file in PHP](https://stackoverflow.com/questions/2805427/how-to-extract-data-from-csv-file-in-php) – Dennis Kozevnikoff Aug 11 '22 at 15:43
  • Why `header("Content-type: multipart/form-data");` when outputting JSON? – Markus Zeller Aug 11 '22 at 15:46
  • Hi, No. My PHP API refuses to acknowledge that a CSV file has been uploaded. $_POST & $_FILES are both empty. When I re-create this issue but use a different file format like JPG, my backend allows me to access the file contents. – Harry Aug 11 '22 at 15:47
  • Hi Markus, my PHP backend returns a JSON object with data like errors or if the operation (that I want to use the csv data for) was successful. – Harry Aug 11 '22 at 15:48

0 Answers0