Single Line - PHP for CSV - php

Emailler.csv:
"email#address1, email#address3, email#address3, ..."
CSVexport.php:
<?PHP
$file_handle = fopen("emailler.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
print $line_of_text[0] . $line_of_text[1] . $line_of_text[2] . "<BR>";
}
fclose($file_handle);
?>
Screen:
"email#address1email#address2email#address3..."
Can I print the screen into single line with php?

I don't know if I'm getting what you want,
but what I guess you want, is
print_r($line_of_text)
This prints all the content of $line_of_text, instead of you needing to call every index on that array.

Related

Problem: register_shutdown_function doesn't start

My part code :
function onDie(){
$filename= 'statistic.txt';
$handle = fopen($filename, 'a+') or die('cannot access file statistic.txt');
fwrite($handle, 'shutdown: ' . mt_rand(1, 100) . PHP_EOL);
fclose($handle);
}
register_shutdown_function('onDie');
//onDie();
echo '1';
die('FAtal error ');
Function onDie didn't run becouse file statistic.txt is empty
What's problem here ? Help please

PHP lock text file for editing?

I've got a form that writes its input to a textfile.
Would it be possible to lock a text file for editing, and perhaps give a friendly message "the file is edited by another user, please try again later."
I'd like to avoid conflicts if the file has multiple editors at the same time.
Here's how the entry is currently added.
$content = file_get_contents("./file.csv");
$fh = fopen("./file.csv", "w");
fwrite($fh, $date_yy . '-' . $date_mm . '-' . $date_dd . '|' . $address . '|' . $person . '|' . $time_hh . ':' . $time_mm);
fwrite($fh, "\n" . $content);
fclose($fh);
Any thoughts?
You can use flock() function to lock the file. For more see this
Something like:
<?php
$content = file_get_contents("./file.csv");
$fp = fopen("./file.csv", "w"); // open it for WRITING ("w")
if (flock($fp, LOCK_EX))
{
// do your file writes here
fwrite($fh, $date_yy . '-' . $date_mm . '-' . $date_dd . '|' . $address . '|' . $person . '|' . $time_hh . ':' . $time_mm);
fwrite($fh, "\n" . $content);
fclose($fh);
flock($fh, LOCK_UN); // unlock the file
}
?>
In order of desirability:
Use a database.
Use more than one text file.
Use locks:
eg:
$lockwait = 2; // seconds to wait for lock
$waittime = 250000; // microseconds to wait between lock attempts
// 2s / 250000us = 8 attempts.
$myfile = '/path/to/file.txt';
if( $fh = fopen($myfile, 'a') ) {
$waitsum = 0;
// attempt to get exclusive, non-blocking lock
$locked = flock($fh, LOCK_EX | LOCK_NB);
while( !$locked && ($waitsum <= $lockwait) ) {
$waitsum += $waittime/1000000; // microseconds to seconds
usleep($waittime);
$locked = flock($fh, LOCK_EX | LOCK_NB);
}
if( !$locked ) {
echo "Could not lock $myfile for write within $lockwait seconds.";
} else {
// write out your data here
flock($fh, LOCK_UN); // ALWAYS unlock
}
fclose($fh); // ALWAYS close your file handle
} else {
echo "Could not open $myfile";
exit 1;
}
You can use PHP's flock function to lock a file for writing, but that lock won't persist across web requests and doesn't work on NFS mounts (at least in my experience).
Your best be may be to create a token file in the same directory, check for its existence and report an error if it exists.
As with any locking scheme, you're going to have race conditions and locks that remain after the operation has completed, so you'll need a way to mitigate those.
I would recommend creating a hash of the file before editing and storing that value in the lock file. Also send that hash to the client as part of the edit form (so it comes back as data on the commit request). Before writing, compare the passed hash value to the value in the file. If they are the same, commit the data and remove the lock.
If they are different, show an error.
You could try flock — Portable advisory file locking ?
http://php.net/manual/en/function.flock.php
I would just use a simple integer or something like that.
$content = file_get_contents("./file.csv");
$fh = fopen("./file.csv", "w");
$status = 1;
...
if($status == 1){
fwrite($fh, $date_yy . '-' . $date_mm . '-' . $date_dd . '|' . $address . '|' . $person . '|' . $time_hh . ':' . $time_mm);
fwrite($fh, "\n" . $content);
fclose($fh);
$status = 0;
}
else
echo "the file is edited by another user, please try again later.";
Is that what you mean?

php input file line by line make first line header one tag

<?php
foreach (glob("POSTS/*.txt") as $filename)
{
$file = fopen($filename, 'r') or exit("Unable to open file!");
//Output a line of the file until the end is reached
echo date('D, M jS, Y H:i a', filemtime($filename))."<br>";
while(!feof($file))
{
echo fgets($file). "<br>";
}
echo "<hr/>";
}
fclose($file);
?>
so this php code reads from a folder all the files and each file line by line, i want it so that the file when being read will: for the first line add an html tag to make the first line a big heading and the next lines normal? how do i do this, thanks
Simply read one line before entering the loop, like this:
echo date('D, M jS, Y H:i a', filemtime($filename))."<br>";
echo '<h1>' . htmlspecialchars(fgets($file)) . '</h1>';
while (!feof($file)) {
echo htmlspecialchars(fgets($file)) . '<br/>';
}
Note that this only works by accident in the case of an empty file, where fgets would return false, which would be shown as ''. You can catch that by explicitely checking for false:
$firstLine = fgets($file);
if ($firstLine == false) {
echo '<h1>' . htmlspecialchars($firstLine) . '</h1>';
}

how to get the text to go to the next row in a csv?

i am trying to get the text to go to the next row in a csv. this works great for TXT but doesnt foe CSV. do i need to write something diffrent for the csv rows?
<?php
$fp = fopen('wo.csv', 'a');
fwrite($fp, '"' . $_POST['Customer'] . '","' . $_POST['Contact_Name'] . '","' . "\r\n");
fclose($fp);
echo "<h1>Thank you for doing business with!</h1>";
?>
Use the built in CSV functionality!
<?php
$fp = fopen('file.csv', 'w');
fputcsv($fp, array($_POST['Customer'], $_POST['Contact_Name']));
fclose($fp);
?>
Reference
fputcsv
fgetcsv

if elseif indie while loop

I am trying to handle a CSV file via php, and I have it working. But there is this one array that I need to change based on a set of conditions.
$file_handle = fopen($path, "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 100000);
if($currency == "US"){
$line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]);
$line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]);
} elseif($currency == "DE"){
$line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]);
$line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]);
}else {
echo "Something with currency handling went wrong. Please contact support.";
}
$data .= $line_of_text[0] . "," . $line_of_text[1] . "," . $line_of_text[2] . "," . $line_of_text[4] . "," . $line_of_text[6] . "," . $line_of_text[49] . "," . $line_of_text[51] . "\n";
}
fclose($file_handle);
$new_file_handle = fopen($path, "w");
fwrite($new_file_handle, $data);
It's not throwing any errors, but seems that the whole conditional block is being ignored. Help?
You're not reading from $file_handle anywhere. feof($file_handle) will never be true. As posted, this code should loop forever.
(BTW, using feof like this is generally not how you want to do it, for this reason among others. Better would be something like while (($line = however_you_read($file_handle)) !== FALSE).) Pretty much all the stream-reading functions return FALSE on any error, or on EOF.)
Check file is correctly opened or no.
Write that code in try catch, so if any error then you can find it.
I think you file doesn't contain anything or it doesn't open successfully.

Categories