i'm pretty new with PHP. So i find this code on the internet and modify it to what i need,but it seems that it gives me one more value in array ,sometimes.
when csv looks like this
a:1
b:2
c:3
d:4
it's fine but when i got
a:1,2,3,4
b:1,2,3,4
c:1,2,3,4
d:1,2,3,4
it adds one more empty line. why is that and can i prevent that?
<?php
$csv_mimetypes = array(
'text/csv',
'text/plain',
'application/csv',
'text/comma-separated-values',
'application/excel',
'application/vnd.ms-excel',
'application/vnd.msexcel',
'text/anytext',
'application/octet-stream',
'application/txt',);
if ($_FILES["csv"]["error"] > 0)
{
echo "<script type='text/javascript'>alert('no file');</script>";
}
elseif(in_array ($_FILES["csv"]["type"] ,$csv_mimetypes))
{
//get the csv file
$file = $_FILES['csv']['tmp_name'];
$csv= file_get_contents($file);
$array = explode("\r\n", $csv);
for($i=0;$i<count($array);$i++){
$array[$i] = explode(';', $array[$i]);
}
echo "<script type='text/javascript'>alert('OK!');</script>";
}
else
{
echo "File must be a .csv";
}
?>
<script type="text/javascript" src="Bar.js"></script>
<script type="text/javascript">
var myJsarray = <?= json_encode($array); ?> ;