I have been trying to read the Xlsx file using fgetcsv concept. Its working for csv, but not for xlsx file
Thing is , its reading the file as like
Array
(
[0] => S����b��wIp� �[�������׀�4�c�W�s�c3�Y�i����Ѐ�& �g���ńA�8���'qt��]�%]>XC�<=�_���G%�����*
)
The website encountered an unexpected error. Please try again later.
and this is my code,
$file = fopen($filePath, 'r');
if($file){
fgetcsv($file, 100000, ",");
while (($example = fgetcsv($file, 100000, ",")) !== FALSE){
echo '<pre>'; print_r($exampe);
}
}
I don't want to use PHPEXCEL library file to read xlsx file, How could I solve above issue.
fgetcsv function is used to read the CSV files only you can't use this for XLS files.
Related
I'm using JSON to handle csv files but when the files are too big my PC starts having problems so I'm asking for a small piece of code on how to read the CSV file and insert it into the database (mysql) line by line without overwhelming the memory
You can use this code and open a stream to read a file:
$newfilename = "redirect.csv";
move_uploaded_file($_FILES["csvfile"]["tmp_name"], "img/" . $newfilename);
$file = fopen($filename,"r");
while(($entry = fgetcsv($file, 50000, ";")) !== FALSE)
{
//your code here
}
I'm trying to read only colum with red label in a csv file. Is there a php function to do this or a symfony bundle?
Now I'm reading csv file with fgetcsv function:
foreach($request->files->get('importFile') as $file) {
if (($handle = fopen($file->getRealPath(), "r")) !== FALSE) {
// get the rest of the rows
$data = array();
$i = 0;
while ($row = fgetcsv($handle, 0, ';')) {
if($i>1) {
$data[] = $row;
$i++;
}
print_r($data);die;
}
}
}
But it doesn't read the label's color.Is there a way to read the color on the csv files?
CSV files have no formatting.
.xls or .odt files have formatting, but CSV definitely not - only data are saved in there. Look at the file with a text editor.
You can use this php class to read csv files: https://git.webworks-nuernberg.de/webworks-nuernberg/parsecsv
But cweiske is right, csv hasn't any formatting.
I am writing a script to basically convert an excel file to JSON. The file is a giant budget with hundreds of lines. I used the PHPExcel library to convert the xlsx file into a csv. I then converted the excel file into a json, but am receiving an error:
Warning: array_combine(): Both parameters should have an equal number of elements
Here is my code
<?php
ini_set('memory_limit', '1000M');
require_once 'classes/PHPExcel/IOFactory.php';
$excel = PHPExcel_IOFactory::load("sap.xlsx");
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setDelimiter(",");
$writer->setEnclosure("");
$writer->setLineEnding("\r\n");
$writer->setSheetIndex(0);
$writer->save("sap.csv");
function getJsonFromCsv($file,$delimiter) {
if (($handle = fopen($file, 'r')) === false) {
die('Error opening file');
}
$headers = fgetcsv($handle, 4000, $delimiter);
$csv2json = array();
while ($row = fgetcsv($handle, 4000, $delimiter)) {
$csv2json[] = array_combine($headers, $row);
}
fclose($handle);
return json_encode($csv2json);
}
$file = 'sap.csv';
echo getJsonFromCsv($file, ',');
What do I need to do to make this work? Or really just convert a large xlsx file to JSON..
Thanks
I need to make CSV file upload for eshop. I have goods in CSV files. They have images defined for example: http://www.servername/pictures/pic1.jpg
I am finding any function, I need that script upload a CSV (solved), open CSV and explode by ",[coma]" (solved), the script go to the link with image [another server], download it and save it into directory at my server.
Colleges helped me, that I should use get_file_contents() function, but on php.net in manual I find another things. How can I solve this problem?
Please try this
if (($handle = fopen("upload/myCSV.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$imageUrl = $data[1];
$contents = file_get_contents(trim($imageUrl));
if ($contents) {
file_put_contents('/path/to/save/pic.jpg', $contents);
}
fclose($handle);
}
In myCSV.csv file data is
"1","http://imagesus.homeaway.com/mda01/5fe39690-1cbf-469d-8525-b946ad1f4ba7.1.10"
The basic procedure is:
// get the image data
$image = file_get_contents('http://www.servername/pictures/pic1.jpg');
// write the image data
$fp = fopen('path_to_your_image_folder/pic1.jpg', 'w'); //not URL
fwrite($fp, $image);
fclose($fp);
You will probably have to add some validation checks to check if the folder is writable, if there is content in $image and so on.
Hi I have a fgetcsv function that reads a CSV file and exports the data into a list. This works, and I have used this function numerous times elsewhere with no problems.
The relevant bits of the code are:
ini_set("auto_detect_line_endings", true);
$file = fopen($_FILES['filename']['tmp_name'],"r");
while (($data = fgetcsv($file, 1000, ",")) !== FALSE)
{
$body_data['user_list'][] = $data;
}
fclose($file);
The problem is that the read CSV stops at any spaces (white space) between words in columned data.
I thought that auto_detect_line_endings would solve this, could it be that this is causing the problem?
Change your while loop with this
while (!feof($file) ) {
$body_data['user_list'][] = fgetcsv($file, 1024);
}