fopen not displaying contents - php

Not sure why my im getting a blank page ? There is data in the .txt and no errors in the php ?
<?php
$fp = fopen("products.txt", "r");
flock($fp, LOCK_SH);
$headings = fgetcsv($fp, 0, "\t");
while ($aLineOfCells = fgetcsv($fp, 0, "\t")) {
$records[] = $aLineOfCells;
}
flock($fp, LOCK_UN);
fclose($fp);
echo $aLineOfCells;
?>
This is the .txt
Product.txt
ID OID Title Description Option Price
01 01JAP Japanese Model This is the japanese option Japanese $3000
02 02ENG English Model This is the english option English $3000

Every time you are echo the last line.
And every $aLineOfCells is an array.You need to take it out.
<?php
$fp = fopen("products.txt", "r");
flock($fp, LOCK_SH);
$headings = fgetcsv($fp, 0, "\t");
$aline = '';
while ($aLineOfCells = fgetcsv($fp, 0, "\t")) {
$records[] = $aLineOfCells[0];
$aline = $aline . $aLineOfCells[0] . "\n";
}
flock($fp, LOCK_UN);
fclose($fp);
echo $aline;
?>

Related

PHP - Convert Tab Delimited TXT file to CSV

I'm trying to convert a tab delimited .txt file into a .csv file.
I was able to use fgetcsv() to open the txt file and get the data for each line with the following code:
$handle = fopen("fileurl.com", "r");
$row = 1;
if (($handle = fopen("fileurl.com", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
print_r($data);
}
fclose($handle);
}
Now i just need to create a csv file from the data array. I've tried using fputcsv(), but haven't had any luck. I've tried something like this, but the csv file it creates isn't correct and only has 1 row:
$fp = fopen('file.csv', 'w');
fputcsv($fp, $data, "\t");
fclose($fp);
An example of how to create a .csv file from the $data array would be great. I've spent a lot of time researching and trying to get this figured out, but haven't been able to get it working.
fputcsv() only writes one line at a time. Not the whole file. You you need to loop through $data in order to add all of that data into your CSV file.
$fp = fopen('file.csv', 'w');
foreach ($data as $line) {
fputcsv($fp, $line);
}
fclose($fp);
A full example using your code:
$handle = fopen("fileurl.com", "r");
$lines = [];
if (($handle = fopen("fileurl.com", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
$lines[] = $data;
}
fclose($handle);
}
$fp = fopen('file.csv', 'w');
foreach ($lines as $line) {
fputcsv($fp, $line);
}
fclose($fp);
This is the correct way to write data into csv file
<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
In fputcsv we use second parameter as an array .
along with it $attachment = mb_convert_encoding($attachment, 'UTF-8', $parts[$i]->parameters[0]->value); also needed for tsv to csv. it will change file type to text/plain

Open txt file grab the number count +1 and save it again

I have a text file number.txt and inside it there is only the number 1.
I'm using the code below to open the file, get the number, add 1, update the content (must be 2) and save it again. My code is not working. Any suggestions?
$fp = fopen('number.txt', 'c+');
flock($fp, LOCK_EX);
$count = (int)fread($fp, filesize('number.txt'));
ftruncate($fp, 0);
fseek($fp, 0);
fwrite($fp, $count + 1);
flock($fp, LOCK_UN);
fclose($fp);
Works for me
<?php
$handle = fopen("test.txt", "r+");
if ($handle) {
$buffer = fgets($handle, 10);
$nCount = (int)$buffer;
rewind($handle );
fputs($handle, $nCount+1);
fclose($handle);
}
?>
You should be able to do this with just
$path = 'number.txt';
file_put_contents($path, 1+(int)file_get_contents($path));

How to insert string before last line?

I saw using fseek to insert string before last line this question, but this isn't solving my problem. I not use "?>" tag. Php version PHP 5.4
example line1
example line2
//i need insert here
lastline $eg};
My code is working but this is adding empty lines after all lines :
$filename = 'example.php';
$arr = file($filename);
if ($arr === false) {
die('Error' . $filename);
}
array_pop($arr);
file_put_contents($filename, implode(PHP_EOL, $arr));
/// I'm deleting last line here
$person = "my text here\n";
file_put_contents($filename, $person, FILE_APPEND);
$person = "andherelastline";
file_put_contents($filename, $person, FILE_APPEND);
//and then add again here
$file = "tmp/saf.txt";
$fc = fopen($file, "r");
while (!feof($fc)) {
$buffer = fgets($fc, 4096);
$lines[] = $buffer;
}
fclose($fc);
//open same file and use "w" to clear file
$f = fopen($file, "w") or die("couldn't open $file");
$lineCount = count($lines);
//loop through array writing the lines until the secondlast
for ($i = 0; $i < $lineCount- 1; $i++) {
fwrite($f, $lines[$i]);
}
fwrite($f, 'Your insert string here'.PHP_EOL);
//write the last line
fwrite($f, $lines[$lineCount-1]);
fclose($f);

How to read out a value from a txt and add it to a drop down list in php?

I write out each line from a txt file where the word 'free' can be found.
<?php
$filename = "data.txt";
$fp = fopen($filename, "r") or die("Couldn't open $filename");
while(!feof($fp))
{ $line = fgets($fp);
if (preg_match('/free/',$line)) // Print the line if it contains the word 'Ravi'
print "$line<br>";
}
fclose($fp);
?>
I would like to add every lines with the word 'free' into a drop down list, where i can pick one and forward this vaule to my email. Is it possible to do?
Yes, it is possible to do so.
Add some echo statements as follows:
$filename = "data.txt";
$fp = fopen($filename, "r") or die("Couldn't open $filename");
echo "<select>"; // THIS ONE
while(!feof($fp)) {
$line = fgets($fp);
if (preg_match('/free/',$line))
echo "<option>" . $line . "</option>"; // THIS ONE
}
echo "</select>"; // AND THIS ONE
fclose($fp);
just build a select-element by php and feed it with $line:
// after $fp = fopen...
$h = '<select name="aname" id="aname">';
// within your while-loop
$h .= '<option value="'.$line.'">'.$line.'</option>';
// after your while-loop
$h .= '</select>';
echo $h;

Improve performance for reading CSV file from ZIP?

Do you have any idea how to improve the performance by reading CSV file from a zip file?
Firstly it open the zip file, then put the data into a memory and then read it by fgetcsv
$zip = new ZipArchive();
if ($zip->open($fileName)) {
$info = $zip->statIndex(0);
$fp = $zip->getStream($info['name']);
if(!$fp) exit("failed\n");
while (!feof($fp)) {
$contents .= fread($fp, 2);
}
fclose($fp);
$zip->close();
}
$temp = fopen("php://memory", "rw");
fwrite($temp, $contents);
fseek($temp, 0);
while (($data = fgetcsv($temp, 0)) !== false) {
....
}
Quick check with php manual showed that this should work:
<?php
$fp = fopen('zip://test.zip#test', 'r'); // test name of file in archive
if (!$fp) {
exit("cannot open\n");
}
while (($data = fgetcsv($fp, 0)) !== false) {
...
}
fclose($fp);

Categories