UTF-8 BOM file upload conversion before Validation |
Hi CodeIgniters,
I am working on a web app with a standard file upload form through jQuery AJAX call. Here's the deal: Files allowed are CSV formatted and can come in both .txt and .csv format. They are exported from Excel which, as we all know, adds BOM to text files. That's why CodeIgniter' validation throws an error, e.g "csvFile does not have a valid file extension.". Everything works fine if I preconvert TXT files to UTF-8 without BOM in Notepad++. What I did was trying to manipulate the $_FILES temp file before passing it to validation. This solution is partly based on the example found here: https://www.memorylack.com/solving-bom-o...nd-header/ Here's the code: HTML Code: <?= form_open_multipart('productdata/importfile', 'class=""'); ?> jQuery Code: $(document).on('click', '#importfile', function() { Custom Helper PHP Code: function fileSandBox($csvFile) Controller PHP Code: public function importFile() $_FILES Code: Array Then, Validation throws "txt_file_no_bom.txt is not a valid uploaded file." I am pretty sure manipulating the $_FILES directly is not a brilliant idea. Do you guys have any suggestion on how to achieve this goal? Thanks! Cheers!
You can turn the BOM off in your programming editor.
Each editor has a different way of doing it so you will have to find yours. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(10-18-2021, 01:42 AM)InsiteFX Wrote: You can turn the BOM off in your programming editor. Thanks! It is the easiest solution, of course. However, these txt/csv files come from external sources I cannot have control over. They contain customer data mostly exported out of Excel. I cannot just tell the user at the other end of the pipeline to take additional steps and reconvert their files because they have BOM ("They have what!? - would they ask..."). And our goal as developers is to find the most user-friendly solution, right? Cheers! |
Welcome Guest, Not a member yet? Register Sign In |