我想做一个函数,返回上传文件的总字数。例如,如果我上传一个大约 13000 字的 doc 文件,该函数将返回 13000 字。现在,我正在使用下面的代码来获取上传的文件并从该文件中获取字数。
$("#filetoTranslation").change(function() {
var formData = new FormData(document.getElementById("validation-form"));
$.ajax({
type: "POST",
url: base_url + "Requestquote/get_file_count",
data: formData,
dataType: "json",
processData: false,
contentType: false,
success: function(result) {
$('#'+result.csrfName).val(result.csrfHash);
$('#wordscount').val(result.wordscount);
}
});
});
public function get_file_count(){
$wordCount = str_word_count(file_get_contents($_FILES['filetoTranslate']['tmp_name']));
$response['error'] = false;
$response['wordscount'] = $wordCount;
$response['csrfName'] = $this->security->get_csrf_token_name();
$response['csrfHash'] = $this->security->get_csrf_hash();
$response['message'] = 'Successful';
echo json_encode($response);
}
我在 sample.doc 文件中粘贴的示例文本如下所示
Lorem ipsum Lorem ipsum dolor sit amet, consectetur adipiscing elit。 Nunc ac faucibus odio.
来自控制器的函数返回 75 insted of 14 作为响应。
然而,即使我选择了一个只有 15 个单词的文件,它也返回了错误的结果。有人帮我解决这个问题吗?