Need help in one scenario in PHP code

F0rce37

Newbie
Joined
May 4, 2020
Messages
35
Reaction score
0
Scenario is first i need to 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..... especially stuck on deletion part and save the file

Need Help!!Thanks in advance
 
You need to read the file in an array, unset the array element (line), then write the array with implode and new line to the file.
 
You need to read the file in an array, unset the array element (line), then write the array with implode and new line to the file

$file = fopen("output.txt","r");
$document = fgets($file);
echo $document;
fclose($document);
header("Location: url&pa=$document&am=1786");
exit();

I did this...but stuck on how to delete only first line from text file and save the file.
 
PHP:
<?php

$filename = 'file.txt';
$url = 'https://www.google.com/';
$am = 1786;

if (file_exists($filename)) {
    $lines = file($filename, FILE_IGNORE_NEW_LINES);
    $pa = $lines[0];

    unset($lines[0]);
  
    file_put_contents($filename, implode("\n", $lines));

    if ($pa != '') {
        header('Location: ' . $url . '&pa=' . $pa . '&am=' . $am);
        exit;
    } else {
        //
    }
} else {
    //
}
 
Back
Top