1

I am trying to send a zip file from my nodejs app to an endpoint written in PHP. I am using axios and formdata

here is my js code:

        const endpoint = '...';
        const form = new FormData();
        form.append('file', fs.createReadStream(zipFilePath), zipFileName);
        axios
            .post(endpoint, form, {
                headers: form.getHeaders()
            })
            .then((res) => {
                console.log(res.data);
            })
            .catch((e) => {
                console.log(e);
            });

I am not getting the file at the php side, $_FILES is always empty.

<?php

header('Access-Control-Allow-Origin: *');

var_dump($_FILES);
die('response from server');
anwar
  • 103
  • 1
  • 10
  • 1
    And `FormData` comes from.... https://www.npmjs.com/package/form-data ? – Mike 'Pomax' Kamermans May 06 '20 at 23:08
  • yes, `const FormData = require('form-data');` – anwar May 06 '20 at 23:14
  • Why not just use the [`submit()`](https://github.com/form-data/form-data#request-submit-params-function-callback-) method? There's also specific [Axios](https://github.com/form-data/form-data#axios) instructions which look slightly different to what you have regarding `headers` (though I can't think why that would make a difference) – Phil May 06 '20 at 23:14
  • Can you show your PHP code? How are you actually verifying that `$_FILES` is empty? When and where do you see that? – Phil May 06 '20 at 23:19
  • 1
    added my php code to the description above. in js I am printing the response from endpoint and it shows me: `array(0) {} response from server` – anwar May 06 '20 at 23:41
  • Thanks for clarifying. Have you checked this article ~ https://www.php.net/manual/features.file-upload.common-pitfalls.php. There are many config properties that effect your ability to handle file uploads in PHP – Phil May 06 '20 at 23:45
  • See if this solves your problem ~ [Why would $_FILES be empty when uploading files to PHP?](https://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php) (ignore the HTML parts obviously) – Phil May 06 '20 at 23:47
  • thanks alot @phil, the link in your last comment was very helpful – anwar May 08 '20 at 00:51

1 Answers1

1

Fixed, it was a php ini issue. Added a .user.ini file and added settings for upload vars. all works now, thanks all.

here are my settings:

[PHP]
default_charset = "UTF-8"
file_uploads = On
max_file_uploads = 20
post_max_size = 256M
memory_limit = 1024M
max_execution_time = 180
upload_max_filesize = 32M
anwar
  • 103
  • 1
  • 10