0

hi i create a simple program when input type file change, file upload to the server and get it by php but in php server doesn't return any data and $_FILES is empty

my form content type is image jpeg and shoudn't change

and request type is put

and never use formdata

here my code:

 document.getElementById('filexxx').addEventListener('change',function(e){
        var formdata = new FormData();

        window.temp = e.target.files[0];
        console.log(e.target.files[0]);
        //https://divar.ir/upload_s3/temp/xxx.jpg
        axios.put('http://localhost:8080/test.php', e.target.files[0], {
                    withToken: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiMDkzNzUyMTA4NTciLCJleHAiOjE2MzY4Nzk1MDcuMDY3MTI2LCJ2ZXJpZmllZF90aW1lIjoxNjM1NTgzNTA3LjA2NzEzMSwidXNlci10eXBlIjoicGVyc29uYWwiLCJ1c2VyLXR5cGUtZmEiOiJcdTA2N2VcdTA2NDZcdTA2NDQgXHUwNjM0XHUwNjJlXHUwNjM1XHUwNmNjIn0.P0sfbX9pg_jOpX2zhf2H5YmIrBdXo-7NSq2ayaJIoQo',
                    headers: {
                        "Content-Type": "image/jpeg"
                    },
                    timeout: null
                })
    });

and my server side code in test.php

<?php
  var_dump($_REQUEST);
  var_dump($_FILES);
taher
  • 159
  • 1
  • 10
  • content-type is wrong. Needs to be multipart/form-data to send files via HTTP upload. Also, use POST instead of PUT, or PHP will not read it automatically. – ADyson Oct 31 '21 at 09:34
  • And you should add the file to `formdata` and then send that. – M. Eriksson Oct 31 '21 at 09:35
  • no i copied this script from a popular website that's work on that the content type image/jpg should't change @ADyson – taher Oct 31 '21 at 09:36
  • Well it's not working for you, is it? Maybe that site had a different backend? See the duplicate for the standard way to do what you're trying to do, enabling PHP to understand the incoming data – ADyson Oct 31 '21 at 09:43
  • i saw that , that's not my question .my question is send file with image/jpeg type @ADyson – taher Oct 31 '21 at 09:44
  • You can't. PHP won't recognise it. Use the standard approach like everyone else. You don't gain anything by using this different content-type. – ADyson Oct 31 '21 at 09:50

0 Answers0