Attempting to upload a different file format to php server

Viewed 34

I'm attempting to upload a file from node js to php an image file works but another file format called "lumi" does not.

The lumi file is basically a json formatted file.

On the js side I have the following:

  const filePath = `${desktopDir}/ai2html-output/${individualFile}`;
  const formFileUpload = fs.readFileSync(filePath);

    // stream file
    let buff = new Buffer(formFileUpload);
    let base64data = buff.toString('base64');

    form.append('file', base64data);
    form.append('name', individualFile);

  axios.post(API_ENDPOINT, form)
  .then(res => console.log(res))

This uploads a jpg image as a base64 and then in the php side I read it and write to a file like so:

$data = file_get_contents('php://input');
$postData = $_POST['file'];
$postName = $_POST['name'];

 file_put_contents('./data/raws/' . $postName, base64_decode($postData));

I then try to execute the same thing for the lumi file with slight variation I don't convert it to a base64.

  const filePath = `${desktopDir}/lumi/${fileName}`;
  const formFileUpload = await fs.promises.readFile(filePath, { encoding: "utf-8" });

  const lumiForm = new FormData();

      lumiForm.append('file', formFileUpload);
      lumiForm.append('name', fileName);
  
  axios.post(API_ENDPOINT, lumiForm)
    .then(res => console.log(res));

This displays a 200 with the file name displaying but it does not write to the server. In this code I check to see if the file name contains "lumi" and then I try to write it.

if( strpos($postName, "lumi") !== false ) {
        file_put_contents('./data/lumi-templates/' . $postName, $postData);
        echo $postData;
}

No file is posted. What am I doing wrong here?

1 Answers

Please check Nginx for allowed file formats, something like below. If you find add lumi as well.

location ~*^.+\.(jpg|jpeg|gif|png|css|zip|pdf|js|ico|html)
Related