Page 1 of 1

File not saving correctly .. why ?

Posted: Thu Nov 26, 2015 3:59 am
by gautamz07
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:

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'); });
}
PHP code:

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);

?>
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.

Re: File not saving correctly .. why ?

Posted: Sun Nov 29, 2015 12:26 pm
by Christopher
On the Javascript side, what are the values of result and fileName before posted? Likewise, what are the values in $_POST on the PHP side?