php excel read returning weird characters - php
I'm using this function to read rows from csv file.
<?PHP
$file_handle = fopen("test.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
echo $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";
}
fclose($file_handle);
?>
And I'm getting this
PK!|l˜l [Content_Types].xml ¢( Ì”]KÃ0†ïÿCÉ4Ù&ˆÈº]øq©çˆÍé–&!'›Û¿÷4û#¤nzÓÐæœ÷}’4ïp¼jL¶„€ÚÙ‚õye`K§´ìmú”ß²£´Jg¡`k#6]^§k˜Q·Å‚Õ1ú;!°¬¡‘ÈK3•ŒôfÂËr.g ½Þ(`c[ 6>#%&f+ú¼! `e÷›ÂÖ«`Ò{£K‰Túæ’o8u¦¬µÇ+Â`¢Ó¡ùÙ`Û÷B[´‚l"C|– aˆ•.Ìß›óÃ"”®ªt Ê•‹†v€£ Ö±1<¼‘Úî¸ø§bi蟤]_>‘cðO8®ÿˆ#Òÿ"=$IæÈ`\À3¯v#z̹–Ôk”gøª}ˆƒîÑ$8”(Nß…]d´Ý¹'!QÃ>4º.ßÞ‘ÒètÃo·Ú¼S :¼EÊ×Ñ'ÿÿPK!µU0#õL_rels/.rels ¢(
Œ’ÏNÃ0ÆïH¼CäûênH¡¥»LH»!TÀ$îµ£$#÷ö„‚JcÛÑöçÏ?[ÞîæiTb/Nú(A±3b{×jxŸV b"giÇŽaWÝÞl_x¤”›b×û¨²‹‹º”ü#b4Oñìr¥‘0QÊahÑ“¨eÜ”å=†¿P-<ÕÁj{ª>ú<ù²·4Mox/æ}b—NŒ#ž;ËvåCf©ÏÛ¨šBËIƒóœÓÉû"cž&Ú\Oôÿ¶8q"K‰ÐHàó<ߊs#ëë.Ÿh©ø½Î<⧄áMdøaÁÅT_ÿÿPK!Þ ý(Ôxl/_rels/workbook.xml.rels ¢(
¼“ÏjÃ0ÆƒÑ}q’ne”:½ŒA¯[÷&QâÐÄ6–ö'o?“Cº#É.¡ƒ$ü}?Чýá§ïÄjU%)´¥«ZÛ(ø8½><ƒ Ö¶Ò³¨`#‚Cq·ÃNsüD¦õ$¢Š%†Ù襤Ò`¯)qmœÔ.ôšcéuyÖ Êõ’ýãšöO /îc)ÇwÚ‡œÝbñÿÿPK!«h&¨bqxl/workbook.xmlŒRËNÃ0¼#ñ–ï4©Ó·šTB€è!QÚ³‰7UÇŽl‡´ÏÆQKQ/œv×;ÏŒ³\+E¾Á:itJ‡ƒ˜Ð¹RïSú¹yy˜Qâ<ׂ+£!¥'pt•Ýß-[c_Æh—ÒÒûzE./¡ân`jи)Œ¸ÇÑî#W[à•¾R‹ãITq©iÏ°°ÿá0E!sx2ySö=‰Å=Êw¥¬Í–…T°í^×o¼BÝGE‰âÎ?éA¤tŒ£iáÏmêÇF*ÜΓ˜Ñ(»˜|·D#Áå7hïÌŽy±c“ÙE±•Ðºßº‘wRÓ¦t4ÃhOç)Á¡ ›¾L)›N稩?{¹/=Ò'É8îØ£+ú ^*ÑÁÝGê_ª«k4€½]HlìZ;†4»BcAß7èä
ýuŽ’r®rŒª+AÄhÓ8h{w†ýK’
ÇcÌxLÚÞœHïÚÖûï]Å›*$A°>–›¸í…J%›•ŠôaËËACMÒÛʈ÷¼ÆJꟉ&Mœ;žÖ4BÎe— tˆYÛ>c~4$”‡–
I have tried many other but getting the same output.
Is there any way I can get string?
Thanks in advance.
This is not a CSV file, it is a ZIP file (note the PK at the beginning).
Probably this is an xlsx file (excel in xml inside a zip package) that you either misnamed or that has the full name test.csv.xlsx but Windows is hiding your real extension (if you are on Windows).
Try to give lineseperator of your csv file like
$csvcontent = fgetcsv($handle, 1000,"\t");
where "\t" is your line seperator.and I think it is not an CSV file...either it is an Zipped file or like .xlsx extended file,First check it once
Related
Encoding CSV File With arabic content
I've tried to import csv file into mysql table and everything is Ok but I get a problem with Encoding the content of the file, the content is in "arabic Content." The following image explains my problem: 1- this is my CSV FILE 2-My PHP code: $c=1; while($data=fgetcsv($file,1000,",")) { if($c==1) { $c++; continue; } $row=explode(";",$data[0]); foreach($row as $val) { echo $val." "; } echo "<br>"; $c++; } 3- The Result: I try to insert data in mysql table 4- If I insert these data into Mysql Table, it will be : I'm thankful for any help, Thanks
You need to convert your CSV file in UTF-8 or process your string using utf8_encode. Make sure your MySQL table is also in UTF-8
/open file pointer to standard output $fp = fopen('php://output', 'w'); //add BOM to fix UTF-8 in Excel fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
Thanks for all answers but the solution is: open CVS file in any editor re-save it with UTF-8 encoding and CVS extension and upload it by an script
Parsing a CSV File
i am trying to convert my xls file into csv and parse it in php Here is sampled converted cvs Windows conversion separated by commas DFRWR2496W/BND*,,2.65 ,2.52 ,3.98 ,910,856,1126, DFWR0006-RD.50W,,2.43 ,2.31 ,3.65 ,380,331,578, DFWR0006-RD.75W,,3.50 ,3.33 ,5.25 ,490,419,775, DFWR0006-RD1.0W,,4.44 ,4.22 ,6.66 ,610,520,972, IN Php i am trying to pase the xxxx.csv file as $file_handle = fopen("price.csv", 'r'); while (!feof($file_handle) ) { $line_of_text[] = fgetcsv($file_handle, 1024,"\r"); } fclose($file_handle); return $line_of_text[0]; However $line_of_text[0] is giving me ["DFRWR2496W\/BND*,,2.65,2.52,3.98,910,856,1126","DFWR0006-RD.50W,,2.43,2.31,3.65,380,331,578","DFWR0006-RD.75W,,3.5,3.33,5.25,490,419,775","DFWR0006-RD1.0W,,4.44,4.22,6.66,610,520,972","DFWR0006-RD1.0Y,,4.44,4.22,6.66,610,520,972","DFWR0006W\/BND,,2.47,2.35,3.71,385,335,586","DFWR0007-RD.50W,,4.54,4.31,6.81,575,483,945","DFWR0007-RD1.0W,,5.28,5.02,7.92,675,568,1105","DFWR0007-RD1.0Y,,5.28,5.02,7.92,675,568,1105","DFWR0007W\/BND,,2.55,2.42,3.83,385,333,593","DFWR0008BD-RD.50W,,4.96,4.71,7.44,825,725,1229","DFWR0008-CSH1.0W,,4.62,4.39,6.93,1075,982,1451","DFWR0008-RD.50W,,4.81,4.57,7.22,1075,978,1467","DFWR0008-RD.75W,,4.81,4.57,7.22,1075,978,1467","DFWR0008-RD1.0W,,4.59,4.36,6.89,1075,982,1449","DFWR0008-TRD7.0W,,5,4.75,7.5,1075,974,1482","DFWR0008W\/BND,,2.23,2.12,3.35,460,415,642","DFWR0012BD-RD.50W,,4.56,4.33,6.84,580,488,951","DFWR0012P-RD.50TTR***,,4.56,4.33,6.84,835,743,1206","DFWR0012-RD.50TTR,,4.31,4.09,6.47,725,638,1076","DFWR0012-RD.50TY,,5.29,5.03,7.94,725,618,1156","DFWR0016W,,2.1,2,3.15,575,533,746","DFWR0028-PRD10x6.5W,,4.05,3.85,6.08,1000,918," Now that is not one line DFRWR2496W/BND*,,2.65 ,2.52 ,3.98 ,910,856,1126, It seems its taking maybe 1024 characters? How am i suppose to parse it like it shows in excel
fgetcsv is not detecting line endings, add this before call to fopen: ini_set('auto_detect_line_endings', true); Processing CSV File in PHP that able to cater MS and UNIX Line Break
read the first line of the txt file then delete that line with php
I have a huge txt file that have 475254 lines and with php I want to read the first line of the my txt file and save it into the Variable and then when I save it the php delete that line. my txt file is about 2.3 MB is it possible to do this?
yes it is /................................. OK less trolling.. You want fopen and fgets will grab a line. REF : fgets Manual PHP $file = "file.txt" $f = fopen($file, 'r'); $line = fgets($f); fclose($f); // You close because you only want the first one. There are so many examples how to do this i feel embarrassed answering. You should show some of what you have tried first! Now you want to remove it: use file_get_contents REF : PHP file_get_contents //Get your file contents $newDoc = file_get_contents($file, true); $newFileContents = substr( $line, strpos($newDoc, "\n")+1 ); //then you want to save it file_put_contents($newFileContents, $file); I might be wrong but you get the idea!~ ;) Process : Get Contents of file Get First Line Replace content of all file with your First Line as New Line Save File Im sure there is a more efficient way to do this, im just winging! NOTE: You may need to configure your php.ini to work with larger files!
yes, Mark is probably too lazy to even try bulid a code, but i have already a working code so.. copypasta $file = "mydata.txt"; $f = fopen($file, 'r'); $line = fgets($f); fclose($f); //do smth $contents = file($file, FILE_IGNORE_NEW_LINES); $first_line = array_shift($contents); file_put_contents($file, implode("\r\n", $contents)); //sleep(1);
PHP Converting CSV to XLS - phpExcel error
I have written an Export script that will export data from my database as a CSV or XLS file. The data from the database needs to be manipulated first. So far I have the CSV file working and am now trying to convert this CSV string to XLS. I want to use phpExcel but get the below error. Can anyone assist? Fatal error: Class 'PHPExcel_Exception' not found in /var/www/leanne/api/library/PHPExcel/Exception.php on line 36 Code which is causing the error, all worked fine until I added the XLS stuff: if($format == 'csv'){ //create and write to file for CSV if(file_exists($file_location . $filename)){ unlink($file_location . $filename); } $fh = fopen($file_location . $filename , 'a'); fwrite($fh, $csv); fclose($fh); } else if($format == 'xls'){ //wite to file for XLS include '../library/PHPExcel/IOFactory.php'; $objReader = PHPExcel_IOFactory::createReader('CSV'); $objReader->setDelimiter($separator); $objReader->setEnclosure(" "); $objReader->setLineEnding($endrow); $objPHPExcel = $objReader->load($csv); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save($file_location . $filename); } NB: csv is a string containing comma separated and double quoted fields from the database. The lines end with '\n'.
Ive downloaded the files again and started from scratch and its working. Must have been something silly I had done, sorry for wasting your time and thanks for the help.
reading a bin file with php bin2hex function
I am trying to read a bin file that contains a lots of two 4-byte numbers in it, which I want to read and convert to hex numbers that is then going to be printed to the screen.... hopefully however I am having a little trouble getting my head around this one. this is what I have so far from reading examples and documentation.. <?php $handle = #fopen("files/bigbin1.bin", "r"); if ($handle) { while (!feof($handle)) { $hex = bin2hex($handle); } fclose($handle); } print_r($hex); ?> I am 95% sure the error is in passing $handle over to tbin2hex.. but this being my first ever reading of a bin file I am slightly lost. the overall goal at some point will be to read the bin file into the database however I am just trying to figure out what this file looks like on screen.
<?php $handle = #fopen("files/bigbin1.bin", "r"); if ($handle) { while (!feof($handle)) { $hex = bin2hex(fread ($handle , 4 )); print $hex."\n"; } fclose($handle); } ?> EDIT: Also you should avoid using # it can make debugging extremely frustrating.