I'm trying to use fgetCsv but for some reason it is only reading the first line. Here is the code:
$fieldseparator = ",";
$lineseparator = "\r";
if(!file_exists($csvFile)) {
echo "<div id='error'>Cannot find uploaded file. Please try again</div>";
exit;
}
$file = fopen($csvFile,"r");
if(!$file) {
echo "<div id='error'>Error loading CSV file</div>";
exit;
}
$size = filesize($csvFile);
if(!$size) {
echo "<div id='warning'>File is empty</div>";
exit;
}
$query = "";
$content = fgetcsv($file,$size,$lineseparator);
fclose($file);
foreach($content as $data) {
$values = explode($fieldseparator,$data);
$query[$i] = "('".implode("','",$values)."')";
}
This just outputs one line. Here is the CSV file:
TSE-P01,1,WO47653897,RM,EcoQuiet,1
TSE-P02,1,WO47653898,RM,EcoQuiet,1
TSE-P03,1,WO47653899,RM,EcoQuiet,1
TSE-P04,1,WO47653900,RM,EcoQuiet,1
TSE-P05,1,WO47653901,RM,EcoQuiet,1
TSE-P06,1,WO47653902,RM,EcoQuiet,1
TSE-P07,1,WO47653903,RM,EcoQuiet,1
TSE-P08,1,WO47653904,RM,EcoQuiet,1
Any ideas why this might be happening?
From the docs:
fgetcsv — Gets line from file pointer and parse for CSV fields
If you want more than one line then you need to call it more than once.
This example is the "Example 2" from w3schools http://www.w3schools.com/php/func_filesystem_fgetcsv.asp
$file = fopen("contacts.csv","r");
while(!feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
Using the while loop the code iterates through the whole file/all the lines..
Alternatively.. if you do something like that..
print_r(fgetcsv($file));
print_r(fgetcsv($file));
print_r(fgetcsv($file));
It will print only the first 3 lines..
Related
Hi I am trying to get this external text file to print inside my php document. The code looks fine to me however when I echo it does not output anything and I am not sure why this is. Can anybody help me out as I am new to this.
$location = '/Applications/MAMP/htdocs/PHPLabs/branches.txt';
$fp = fopen($location, 'r');
if ($fp) {
$readin = fread($fp);
fclose($fp);
} else {
echo 'Can\'t open input.txt';
}
Not sure what you're trying to 'echo' but have you checked if the file exists in the first place?
Your code could be written as:
$location = '/Applications/MAMP/htdocs/PHPLabs/branches.txt';
if (file_exists($location) && $data = file_get_content($location)){
echo $data;
} else {
echo 'File not found';
}
if (file_exists($location) && $file = fopen($location, 'r')){
$file_content = fread($file, filesize($location));
fclose($file);
} esle {
echo 'File not found';
}
See here for more: http://php.net/manual/en/function.file-get-contents.php, http://php.net/manual/en/function.filesize.php
I am attempting to echo the bottom line only of a .csv file. I echo the file in reverse order (as shown below) but I'm not sure how to only echo the top line of this. Please note that the csv variable, constantly changing.
Here is my php:
<?php
$file = file("file.csv");
$file = array_reverse($file);
foreach($file as $f){
echo $f."<br />";
}
?>
Simple solution is.
$file = "file.csv";
$data = file($file);
$last_line = $data[count($data)-1];
Maybe try the end function see http://php.net/manual/en/function.end.php
<?php
$file = file("file.csv"); // You take care of reading all the lines.
$lastline = end($file);
?>
<?php
$file = file("file.csv");
$last_line=count($file)-1;
echo $file[$last_line];
?>
I have a csv file placed at path
#http://thevowapp.com/brandstore/values.csv
$f = fopen("http://thevowapp.com/brandstore/values.csv", "w+");
if(!f)
{
echo "Error";
}
$line = fgetcsv($f);
echo json_encode($line);
I am trying to parse it, however the fgetCsv keeps on returning null. What could be the error?
Problems:
You try to open a remote file with w+ (write) access. Use r for read.
You check f (undefined constant). Use$f`.
You don't loop fgetcsv, so you won't get more than the header line.
Try:
$f = fopen('http://thevowapp.com/brandstore/values.csv', 'r');
if(!$f) {
echo 'Error';
exit;
}
$out = array();
while ($line = fgetcsv($f)) {
$out[] = $line;
}
echo json_encode($out, JSON_PRETTY_PRINT);
Remove the pretty print option when you're happy.
I have a textfile ($file) that contains the path to other files. My goal is to read the content of those files and print it in a table. When reading the paths from $file, only the last line path works correctly.
<?php
$file = "log2.txt";
if(file_exists($file)) {
$handler = fopen($file,'r');
while(!feof($handler)) {
$lines = fgets($handler);
$wordarray = explode(' ', $lines);
#echo $wordarray[0]." ".$wordarray[1]." ".$wordarray[2];
if (strpos($lines, 'NOK') !== false) {
echo "<tr><td>".$wordarray[0]."</td></tr>";
if(file_exists($wordarray[2])){
$log = fopen($wordarray[2], 'r');
#echo "FILE EXISTS ".$log;
$logtext = fread($log,filesize($wordarray[2]));
echo "<tr><td>".$logtext."</td></tr>";
fclose($log);
} else {
echo "<tr><td>"."FILE ".$wordarray[2]." FAILED TO LOAD"."</td></tr>";
}
}
}
fclose($handler);
} else {
echo "FILE DOES NOT EXISTS";
}
?>
Here is an exemple of how log2.txt would look like:
POE NOK poelog.txt
LINK-ERRORS OK OK
LATENCIES NOK latencieslog.txt
VOLATILE NOK volatilelog.txt
I think that the problem could be about line endings or so, but cannot get the point.
You're right. The $wordarray[2] contains also new line character so before using it pass it through trim() function and store it in a variable ($filename in my case).
Updated inner if:
if (strpos($lines, 'NOK') !== false) {
echo "<tr><td>".$wordarray[0]."</td></tr>";
$filename = trim($wordarray[2]);
if (file_exists($filename)){
echo "FILE EXISTS ".$filename;
$log = fopen($filename, 'r');
$logtext = fread($log, filesize($filename));
echo "<tr><td>".$logtext."</td></tr>";
fclose($log);
}
else{
echo "<tr><td>"."FILE ".$filename." FAILED TO LOAD"."</td></tr>";
}
}
This short function works like a sharm:
function displayFileContent( $fileName )
{
$arrayWithFileNames = file ( $fileName );
echo "<table>";
foreach ( $arrayWithFileNames as $singleFileName )
{
# remove the trailing \n on Linux - windows has 2 character as EOL
$singleFileName = trim(preg_replace('/\s\s+/', ' ', $singleFileName));
$contentOfFile = file_get_contents( $singleFileName );
echo "<tr><td>{$contentOfFile}</td></tr>";
}
echo "</table>";
}
You use it like this:
displayFileContent ("path-to-your-file");
Remark: There is no check if the file does exist....
I am having some difficulty with reading info from a text file. Is it possible to use php and get one line at a time, and compare that line to a variable, one character at a time? Every time I add the character searching algorithm it messes up. or does the file reading only do full files/lines/character
ex:
$file=fopen("text/dialogue.txt","r") or exit("unable to open dialogue file");
if($file == true) {
echo "File is open";
fgets($file);
$c = "";
while(!feof($file)) {
$line = fgets($file)
while($temp = fgetc($line)) {
$c = $c . $temp;
//if statement and comparrison
}
}
} else {
echo "File not open";
}
fclose($file);
You may use php file function to read a file line by line
<?php
$lines = file("myfile.txt");
foreach($lines as $line){
## do whatever you like here
echo($line);
}
?>
Please check php manual
http://php.net/manual/en/function.file.php