File not saving correctly .. why ?
Posted: Thu Nov 26, 2015 3:59 am
I was just going through this answer Here http://stackoverflow.com/a/4006992/4381665 , its about file uploading using the File API , the code i have used in my application , is slightly modified, and looks like so:
JS code:
PHP code:
Yet for some reason , my jpeg does't get saved properly , I.E. it is not accessable, WHY ? in the comments of that thread the author mentions a few more steps he implemented to get it working , but i am unable to follow, can somebody enlighten me ?
I believe the error is in the PHP code somewhere.
Thank you.
JS code:
Code: Select all
$(function(){
$('button[type="submit"]').on('click' , function(e){
e.preventDefault();
var file = document.getElementById('fileBox').files[0]; //Files[0] = 1st file
var reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = shipOff;
//reader.onloadstart = ...
//reader.onprogress = ... <-- Allows you to update a progress bar.
//reader.onabort = ...
//reader.onerror = ...
//reader.onloadend = ...
});
});
function shipOff(event) {
var result = event.target.result;
var fileName = document.getElementById('fileBox').files[0].name; //Should be 'picture.jpg'
console.log(fileName);
$.post('fileupload.php', { data: result, name: fileName }, function(){ console.log('yeeey'); });
}
Code: Select all
<?php
$data = $_POST['data'];
$fileName = $_POST['name'];
$serverFile = time().$fileName;
$fp = fopen('uploads/'.$serverFile,'w'); //Prepends timestamp to prevent overwriting
fwrite($fp, $data);
fclose($fp);
$returnData = array( "serverFile" => $serverFile );
echo json_encode($returnData);
?>
I believe the error is in the PHP code somewhere.
Thank you.