Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/53a2c370de2c812978f4050fafddf75b to your computer and use it in GitHub Desktop.
Save code-boxx/53a2c370de2c812978f4050fafddf75b to your computer and use it in GitHub Desktop.
PHP AJAX Image Upload

PHP AJAX IMAGE UPLOAD

https://code-boxx.com/php-ajax-image-upload/

NOTES

  1. Use 1a-upload.html single image upload, or 2a-upload.html for multiple image uploads.
  2. Captain Obvious - Use http:// and not file://.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>
AJAX Image Upload Demo
</title>
<link rel="stylesheet" type="text/css" href="x-theme.css">
<script src="1b-upload.js"></script>
</head>
<body>
<form id="upForm" onsubmit="return up();">
<input type="file" accept="image/*" name="upFile" required>
<input type="submit" value="Upload">
</form>
</body>
</html>
function up () {
// (A) GET SELECTED IMAGE
var data = new FormData(document.getElementById("upForm"));
// (B) AJAX UPLOAD
fetch("1c-upload.php", { method:"POST", body:data })
.then(res => res.text())
.then(txt => alert(txt))
.catch(err => console.error(err));
return false;
}
<?php
$src = $_FILES["upFile"]["tmp_name"];
$dst = $_FILES["upFile"]["name"];
echo move_uploaded_file($src, $dst) ? "OK" : "ERROR" ;
<!DOCTYPE html>
<html>
<head>
<title>
AJAX Image Upload Demo
</title>
<link rel="stylesheet" type="text/css" href="x-theme.css">
<script src="2b-upload.js"></script>
</head>
<body>
<!-- (A) UPLOAD FORM -->
<form id="upForm" onsubmit="return up.add();">
<input type="file" accept="image/*" id="upFile" required multiple>
<input type="submit" value="Upload" id="upGo" disabled>
</form>
<!-- (B) UPLOAD STATUS -->
<div id="upStat"></div>
</body>
</html>
var up = {
// (A) INIT
upForm : null, // html form
upFile : null, // html file selector
upGo : null, // html submit button
upStat : null, // html status display
queue : [], // upload queue
init : () => {
up.upForm = document.getElementById("upForm");
up.upFile = document.getElementById("upFile");
up.upGo = document.getElementById("upGo");
up.upStat = document.getElementById("upStat");
up.upGo.disabled = false;
},
// (B) ADD TO UPLOAD QUEUE
add : () => {
// (B1) DISABLE SUBMIT BUTTON
up.upGo.disabled = true;
// (B2) GET FILES + RESET FIELD
for (let f of up.upFile.files) { up.queue.push(f); }
up.upForm.reset();
// (B3) AJAX UPLOAD
up.ajax();
return false;
},
// (C) AJAX UPLOAD
ajax : () => {
// (C1) APPEND FIRST FILE IN QUEUE
var data = new FormData();
data.append("upFile", up.queue[0]);
// (C2) UPLOAD
fetch("2c-upload.php", { method:"POST", body:data })
.then(res => res.text())
.then(txt => {
// (C2-1) SHOW UPLOAD STATUS
up.upStat.innerHTML += `<div>${up.queue[0].name} - ${txt}</div>`;
// (C2-2) NEXT FILE
up.queue.shift();
if (up.queue.length!=0) { up.ajax(); }
else { up.upGo.disabled = false; }
})
.catch(err => {
// (C2-3) ERROR!
up.upStat.innerHTML += `<div>${err.message}</div>`;
console.error(err);
});
}
};
window.onload = up.init;
<?php
$src = $_FILES["upFile"]["tmp_name"];
$dst = $_FILES["upFile"]["name"];
echo move_uploaded_file($src, $dst) ? "OK" : "ERROR" ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment