I'm trying to code my own php uploader so I can learn and understand how it all works. Trying not to use any libraries/plugins/API's. So basically I have the javascript stuff doin what it's supposed to be (i hope..). Now I'm stuck on the PHP. I want it to upload and save it as img/1.png. (not using any variables just to test it...). Here are my scripts. Keeping it very simple without any error checks just to get basic uploading working. Would this work for multiple files? May somebody guide me on what my PHP file should look like just for very basic (multi)file upload? Thanks!
album_create.js
$(document).ready(function() {
$('#album_create').submit(function() {
var file = document.getElementById('album-files').files[0]; //Files[0] = 1st file
var reader = new FileReader();
reader.onload = function(f) {shipOff(f);};
reader.onerror = function(event) {
console.error("File could not be read! Code " + event.target.error.code);
};
reader.readAsText(file, 'UTF-8');
return false;
});
});
function shipOff(event) {
var result = event.target.result;
var fileName = document.getElementById('album-files').files[0].name; //Should be 'picture.jpg'
console.log(fileName);
$.post('ajax/album_create.php', { data: result, name: fileName }, function(data){console.log(data);});
return false;
}
ajax/album_create.php
$data = $_POST['data'];
move_uploaded_file($data[0], 'img/1.png');