0

function fileupload () {

    var data = new FormData(),
    PMfile = document.getElementById("PMfile").files;
    data.append("file", PMfile[0]);

    var xhr = new XMLHttpRequest();
    xhr.open("POST", "upload.php", true);
  
    xhr.send(data);
    return false;
}
<input onchange="fileupload()" type="file" id="PMfile" name="PM_file" class="form-control" required>

I want to get the file name at upload.php I tried $filename = $_FILES['file']['name']; but this doesn't work.

James Westgate
  • 11,306
  • 8
  • 61
  • 68
  • Does this answer your question? [Get the filename of a fileupload in a document through JavaScript](https://stackoverflow.com/questions/1804745/get-the-filename-of-a-fileupload-in-a-document-through-javascript) – Altherius Jul 07 '20 at 15:54

1 Answers1

0

This is correct and should work. Dump whole $_FILES and inspect if you get what you want. For me it displays this:

Array ( [file] => Array ( [name] => example.jpg [type] => image/jpeg [tmp_name] => /tmp/phpBQOHQl [error] => 0 [size] => 48316 ) )

So $_FILES['file']['name'] is going to be example.jpg

But if i upload a file that is too big for my current php settings i get $_FILES as Array ( ) so it is empty and in apache logs i have:

[Tue Jul 07 21:04:25.535907 2020] [:error] [pid 7729] [client 127.0.0.1:37234] PHP Warning:  POST Content-Length of 21138710 bytes exceeds the limit of 8388608 bytes in Unknown on line 0, referer: http://localhost/a.php

So my guess is - code is correct (if there are no errors in js/html you dont show us) but upload has an error - check your error logs.

blahy
  • 1,294
  • 1
  • 8
  • 9