How to upload files using xmlHTTPrequest2 and php?
Below I have some javascript code that uploads a file to upload.php via
xmlHTTPrequest2. However I have run the program multiple times and it
seems that the PHP file does not recieve the file send through ajax.
I'm not sure where I have gone wrong, but could someone help me fix my
problem?
Javascript
var ts = Math.round((new Date()).getTime() / 1000);
xhr.open("POST", "upload.php", true);
xhr.setRequestHeader("FILENAME", ts);
xhr.send(file);
PHP
$fn = (isset($_SERVER['HTTP_FILENAME']) ? $_SERVER['HTTP_FILENAME'] : false);
if ($fn) {
// AJAX call
// new filename. lets call it date(Y-m-d H:i:s)
file_put_contents(
'files/filesgetcontent/fhub/' . $fn,
file_get_contents('php://input')
);
echo "$fn uploaded";
exit();
}
No comments:
Post a Comment