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

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

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 :

const formData = new FormData();
formData.append("file", droppedFile);

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

the PHP file :

<?php 

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: X-Requested-With');

ini_set('file_uploads', 'On');
ini_set('upload_max_filesize', '1GB');
ini_set('post_max_size', '1GB');

$path = "../models/";

if(isset($_POST)){
   var_dump($_FILES); // PROBLEM HERE

    // $location = $path . 'fbx/' .$filename;  
    // if(!file_exists($location)){
    //    if(!move_uploaded_file($_FILES['file']['tmp_name'], $location))
    //    {
    //      echo json_encode("File was not saved");
    //      exit(1);
    //    }
    //    echo json_encode("file was created");
    // }
    // else{
    //    echo json_encode("file already exists");
    // }
}
else{
    echo json_encode("no POST data");
}
英文:

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 :

 const formData = new FormData();
 formData.append(&quot;file&quot;, droppedFile);

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

the PHP file :

&lt;?php 

header(&#39;Access-Control-Allow-Origin: *&#39;);
header(&#39;Access-Control-Allow-Methods: GET, POST&#39;);
header(&#39;Access-Control-Allow-Headers: X-Requested-With&#39;);

ini_set(&#39;file_uploads&#39;, &#39;On&#39;);
ini_set(&#39;upload_max_filesize&#39;, &#39;1GB&#39;);
ini_set(&#39;post_max_size&#39;, &#39;1GB&#39;);

$path = &quot;../models/&quot;;

if(isset($_POST)){
   var_dump($_FILES); // PROBLEM HERE

    // $location = $path . &#39;fbx/&#39; .$filename;  
    // if(!file_exists($location)){
    //    if(!move_uploaded_file($_FILES[&#39;file&#39;][&#39;tmp_name&#39;], $location))
    //    {
    //      echo json_encode(&quot;File was not saved&quot;);
    //      exit(1);
    //    }
    //    echo json_encode(&quot;file was created&quot;);
    // }
    // else{
    //    echo json_encode(&quot;file already exists&quot;);
    // }
}
else{
    echo json_encode(&quot;no POST data&quot;);
}

答案1

得分: 0

尝试这样做:

const formData = new FormData();
formData.append("file", droppedFile);
formData.append("extension", ${droppedFile.name.split(".").pop());

// 发送 POST 请求
const saveFile = await fetch("../controller/save-file.php", {
    method: "POST",
    body: formData
}).then((response) => response.json());
$_POST['extension'] = ....
$_FILES = ....
英文:

Try this:

 const formData = new FormData();
 formData.append(&quot;file&quot;, droppedFile);
 formData.append(&quot;extension&quot;,${droppedFile.name.split(&quot;.&quot;).pop());
     
//post request
const saveFile = await fetch(&quot;../controller/save-file.php&quot;, {
    method: &quot;POST&quot;,
    body: formData
}).then((response) =&gt; response.json());
 $_POST[&#39;extension&#39;] = ....
 $_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:

确定