I try to read file (id="file") after selected and return character count to paragraph (id="count"). I want it without submitting, without clicking any other button and without saving. I've tried jquery and "vanilla" javascript. Here is my code with "vanilla":
HTML:
<input type="file" id="file" name="file" onchange="fileCount(this)" />
<p id="count"></p>
Javascript:
function fileCount(file) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("count").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open('POST', 'fileCount.php', true);
xmlhttp.setRequestHeader('X-FileName',file.name);
xmlhttp.send(file);}
PHP:
if(!empty($_FILES['file'])) {
if ($_FILES['file']['error'] === 0) {
$temp = $_FILES['file']['tmp_name'];
$ext = (explode(".",$temp));
$ext = end($ext);
$file = "";
switch ($ext) {
case "pdf":
$text = pdf2text($temp);
$file = strlen(utf8_decode($text));
print_r( "<p id='count'>Počet: " . $file . "</p>");
break;
case "docx":
$text = docx2text($temp);
$file = strlen(utf8_decode($text));
print_r( "<p id='count'>Počet: " . $file . "</p>");
break;
case "odt":
$text = odt2text($temp);
$file = strlen(utf8_decode($text));
print_r( "<p id='count'>Počet: " . $file . "</p>");
break;
case "rtf":
$text = rtf2text($temp);
$file = strlen(utf8_decode($text));
print_r( "<p id='count'>Počet: " . $file . "</p>");
break;
//case "txt":
$text = file_get_contents($temp);
$file = strlen(utf8_decode($text));
print_r( "<p id='count'>Počet: " . $file . "</p>");
break;
default:
print_r( "Wrong file format" );
}
}
}
and nothing is returning. Please help, what is wrong. Thank you