this is my script which works perfectly on php 8.0, but on php 7.4 its malfunctioning.
$fileName = 'Usecase.csv';
$tempName = 'temp.csv';
$inFile = fopen($fileName, 'r');
$outFile = fopen($tempName,'w');
while (($line = fgetcsv($inFile)) !== FALSE)
{
if(($line[0] == "$fin") )
{
$line = explode (",", "$tempstr10");
$asd=$asd+1;
}
fputcsv($outFile, $line );
}
fclose($inFile);
fclose($outFile);
unlink($fileName);
rename($tempName, $fileName);
if( $asd==0 && filesize("Usecase.csv")>0)
{ file_put_contents("Usecase.csv", "$tempstr10\r\n",FILE_APPEND | LOCK_EX); }
if( $asd==0 && filesize("Usecase.csv")==0)
{ file_put_contents("Usecase.csv", "$tempstr10\r\n",FILE_APPEND | LOCK_EX); }
I tried printing the error if any , but nothing was printed.
1: So whats the best practice to track an issue like this ? 2: according to my limited knowledge this script uses simple functions which are supported by both php7 and php8 so why this issue ?
Thank you
Update: this part of the code reads a csv file and if the csv file contains a certain string then it will replace the entire row that the string was found in.
$fileName = 'Usecase.csv';
$tempName = 'temp.csv';
$inFile = fopen($fileName, 'r');
$outFile = fopen($tempName,'w');
while (($line = fgetcsv($inFile)) !== FALSE)
{
if(($line[0] == "$fin") )
{
$line = explode (",", "$tempstr10");
$asd=$asd+1;
}
fputcsv($outFile, $line );
}
fclose($inFile);
fclose($outFile);
unlink($fileName);
rename($tempName, $fileName);
technically $line[0] == "$fin"
checks if the value of $fin matches the $line[0], if yes the replace the entire $line with $tempstr10
if string is matched then it will increment the value of the $asd and the next part of the code wont be executed
otherwise this will be executed based on the if statement :
if( $asd==0 && filesize("Usecase.csv")>0)
{ file_put_contents("Usecase.csv", "$tempstr10\r\n",FILE_APPEND | LOCK_EX); }
if( $asd==0 && filesize("Usecase.csv")==0)
{ file_put_contents("Usecase.csv", "$tempstr10\r\n",FILE_APPEND | LOCK_EX); }
Issue : In php8 when a string already existed in csv file it would replace that entire row (the row that contained the string ) with the new string but in php7 it doesnt replace it just adds the new line with the new value