Sending POST request from JS to PHP – $_FILES is NULL, any ideas why?

huangapple go评论118阅读模式
英文:

Sending POST request from JS to PHP - $_FILES is NULL, any ideas why?

问题

I am sending a POST request from a native JS file to a PHP file.

At some point, I was able to access my $_FILES var normally, without any problems; however, I added a second param (extension) and now it simply is NULL.
I was able to take the file from $_FILES and save it on my server.

var_dump($_POST) prints the [Object FormData], but var_dump($_FILES) prints NULL (not empty).

Any ideas? Thank you very much

the JS file :

  1. const formData = new FormData();
  2. formData.append("file", droppedFile);
  3. // post request
  4. const saveFile = await fetch("../controller/save-file.php", {
  5. method: "POST",
  6. headers: {
  7. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  8. },
  9. body: `formData=${formData}&extension=${droppedFile.name.split(".").pop()}`
  10. }).then((response) => response.json());

the PHP file :

  1. <?php
  2. header('Access-Control-Allow-Origin: *');
  3. header('Access-Control-Allow-Methods: GET, POST');
  4. header('Access-Control-Allow-Headers: X-Requested-With');
  5. ini_set('file_uploads', 'On');
  6. ini_set('upload_max_filesize', '1GB');
  7. ini_set('post_max_size', '1GB');
  8. $path = "../models/";
  9. if(isset($_POST)){
  10. var_dump($_FILES); // PROBLEM HERE
  11. // $location = $path . 'fbx/' .$filename;
  12. // if(!file_exists($location)){
  13. // if(!move_uploaded_file($_FILES['file']['tmp_name'], $location))
  14. // {
  15. // echo json_encode("File was not saved");
  16. // exit(1);
  17. // }
  18. // echo json_encode("file was created");
  19. // }
  20. // else{
  21. // echo json_encode("file already exists");
  22. // }
  23. }
  24. else{
  25. echo json_encode("no POST data");
  26. }
英文:

I am sending a POST request from a native JS file to a PHP file.

At some point, I was able to access my $_FILES var normally, without any problems ; however, I added a second param (extension) and now it simply is NULL.
I was able to take the file from $_FILES and save it in my server.

var_dump($_POST) prints the [Object FormData], but var_dump($_FILES) prints NULL (not empty).

Any ideas? Thank you very much

the JS file :

  1. const formData = new FormData();
  2. formData.append(&quot;file&quot;, droppedFile);
  3. //post request
  4. const saveFile = await fetch(&quot;../controller/save-file.php&quot;, {
  5. method: &quot;POST&quot;,
  6. headers: {
  7. &quot;Content-Type&quot;: &quot;application/x-www-form-urlencoded; charset=UTF-8&quot;,
  8. },
  9. body: `formData=${formData}&amp;extension=${droppedFile.name.split(&quot;.&quot;).pop()}`
  10. }).then((response) =&gt; response.json());

the PHP file :

  1. &lt;?php
  2. header(&#39;Access-Control-Allow-Origin: *&#39;);
  3. header(&#39;Access-Control-Allow-Methods: GET, POST&#39;);
  4. header(&#39;Access-Control-Allow-Headers: X-Requested-With&#39;);
  5. ini_set(&#39;file_uploads&#39;, &#39;On&#39;);
  6. ini_set(&#39;upload_max_filesize&#39;, &#39;1GB&#39;);
  7. ini_set(&#39;post_max_size&#39;, &#39;1GB&#39;);
  8. $path = &quot;../models/&quot;;
  9. if(isset($_POST)){
  10. var_dump($_FILES); // PROBLEM HERE
  11. // $location = $path . &#39;fbx/&#39; .$filename;
  12. // if(!file_exists($location)){
  13. // if(!move_uploaded_file($_FILES[&#39;file&#39;][&#39;tmp_name&#39;], $location))
  14. // {
  15. // echo json_encode(&quot;File was not saved&quot;);
  16. // exit(1);
  17. // }
  18. // echo json_encode(&quot;file was created&quot;);
  19. // }
  20. // else{
  21. // echo json_encode(&quot;file already exists&quot;);
  22. // }
  23. }
  24. else{
  25. echo json_encode(&quot;no POST data&quot;);
  26. }

答案1

得分: 0

尝试这样做:

  1. const formData = new FormData();
  2. formData.append("file", droppedFile);
  3. formData.append("extension", ${droppedFile.name.split(".").pop());
  4. // 发送 POST 请求
  5. const saveFile = await fetch("../controller/save-file.php", {
  6. method: "POST",
  7. body: formData
  8. }).then((response) => response.json());
  1. $_POST['extension'] = ....
  2. $_FILES = ....
英文:

Try this:

  1. const formData = new FormData();
  2. formData.append(&quot;file&quot;, droppedFile);
  3. formData.append(&quot;extension&quot;,${droppedFile.name.split(&quot;.&quot;).pop());
  4. //post request
  5. const saveFile = await fetch(&quot;../controller/save-file.php&quot;, {
  6. method: &quot;POST&quot;,
  7. body: formData
  8. }).then((response) =&gt; response.json());
  1. $_POST[&#39;extension&#39;] = ....
  2. $_FILES = ....

huangapple
  • 本文由 发表于 2023年5月29日 16:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定