Need help in one scenario in PHP code

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
electron37
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Jun 04, 2021 2:10 am

Fri Jun 04, 2021 2:16 am

Read first line of text file and store it in variable and pass that stored variable with some more data to location header for post method.After it,delete the first line in text file and update the file.So that every time code execute or url gets hit,user gets new data from text file.
Totally confused how to proceed.....

Need Help!!Thanks in advance
AdoptiveSolution
php-forum GURU
php-forum GURU
Posts: 212
Joined: Wed Jun 15, 2016 8:35 am

Fri Jun 04, 2021 11:52 am

Based on this :

https://stackoverflow.com/a/9046682

I created this :

Code: Select all

<?php

function read_and_delete_first_line( $filename )
{
	$file   = file( $filename );
	$output = $file[0];
	unset( $file[0] );
	file_put_contents( $filename, $file );
	return $output;
}

$csvfile = 'aka.csv';
$aka     = file( $csvfile );
echo '<pre>' . print_r( $aka, TRUE ) . '</pre>';

if ( !empty( $aka ) )
{
	echo '<p>Deleted : <b>' . read_and_delete_first_line( $csvfile ) . '</b></p>';

	$aka     = file( $csvfile );
	echo '<pre>' . print_r( $aka, TRUE ) . '</pre>';

} else {
	echo '<p>File <b>' . $csvfile . '</b> is empty</p>';
}
?>

<p><button><a href="?">Again</a></button></p>
Post Reply