Jump to content

Recommended Posts

I'm PHP newbie and need help with correction or logic to have this script work.

I open a config file, put contents in array $arLines then walk through the array line by line using foreach $arLines $key => $value. I want to unset the lines in $arLines for which $_GET("Line"] variables passed from a form exist ie

on the display a delete link has been activated. Thank you in advance.

 

<?php

//print_r($_GET);

//line to delete
if (isset($_GET["Line"]))
{
$nLine = $_GET["Line"];
}//end if (isset($_GET["Line"]))
else
{
die();
}//end else (isset($_GET["Line"]))

$strFilename = "page.conf";
$nFileHandle = fopen($strFilename, "r+");

//read all lines into array ("file"-command)
// delete the line we want

while (!feof($nFileHandle))
{
$arLines = file($nFileHandle);

foreach ($arLines as $Key => $value)
{

if(isset($nLine[]))
{
unset($key,$value);
}// end of if(!$arLines["delete"]==false)
}// end of foreach ($arLines as $Key => $value)
}// end of while (!feof($nFileHandle))
?>

Link to comment
https://forums.phpfreaks.com/topic/69872-deleting-lines-in-a-file/
Share on other sites

You don't need the fopen() and the while loop. Should be something like this (although I'm not too sure what the condition for deleting should be from your description)

 

<?php

//print_r($_GET);

//line to delete
if (isset($_GET["Line"]))
{
    $nLine = $_GET["Line"];
}//end if (isset($_GET["Line"]))
else
{
    die();
}//end else (isset($_GET["Line"]))

$strFilename = "page.conf";
//read all lines into array ("file"-command)
// delete the line we want

$arLines = file($strFilename );

foreach ($arLines as $Key => $value)
{
    if(trim($value)==$nline)                             // or whatever the condition is
    {
        unset($arLines[$Key]);
    }// end of if(!$arLines["delete"]==false)
}// end of foreach ($arLines as $Key => $value)
?>

 

If deletions are to be permanent you now need to re-write the file.

Thank you Barand and Hemlata. Still got a problem. This script wipes out entire file  :'(  Any idea why?

Please pardon my ignorance.

 

 

<?php

 

//print_r($_GET);

 

//line to delete

    if (isset($_GET["Line"]))

    {

      $nLine = $_GET["Line"];

    }//end if (isset($_GET["Line"]))

    else

    {

      die();

    }//end else (isset($_GET["Line"]))

 

    $strFilename = "page.conf";

    $arLines = file($strFilename); 

    foreach ($arLines as $key => $value)

    {

      $result = array_search($nLine, $arLines);

    if (false !== $result)

    {

    unset($arLines[$result]);

    }// end of f (false !== $result)

    }// end of foreach ($arLines as $key => $value)

   

   

    //write new page.conf

    $nFileHandle = fopen("page.conf" "w");

    $i=0;

    $nMaxlength = count($arLines);

    for ($i=0; $i<$nMaxlength; $i++)

    {

    $arCurrentLine = $arLines[$i];

    $strNewLine ="";

     

  // add URL to string

    $strNewLine = $strNewLine.$arLines["url"]."\t";

    $strNewLine = $strNewLine.$arLines["email"]. "\t";

    $strNewLine = $strNewLine.$arLines["size"]. "\t";

    $strNewLine = $strNewLine.$arLines["command"]. "\r\n";

    fwrite($nFileHandle,$strNewLine);

     

    }//end for ($i=0; $i<$nMaxlength; $i++)

    fclose($nFileHandle); 

   

?>

Hello,

 

In the following code block, you are actually replacing value all the time instead of concatenating it.

 

 

  $strNewLine = $strNewLine.$arLines["url"]."\t";
    $strNewLine = $strNewLine.$arLines["email"]. "\t";
    $strNewLine = $strNewLine.$arLines["size"]. "\t";
    $strNewLine = $strNewLine.$arLines["command"]. "\r\n";

 

use the below code and see whether this solves your problem or not

 

    $strNewLine = $strNewLine.$arLines["url"]."\t";
    $strNewLine .= $strNewLine.$arLines["email"]. "\t";
    $strNewLine .= $strNewLine.$arLines["size"]. "\t";
    $strNewLine .= $strNewLine.$arLines["command"]. "\r\n";

 

Regards,

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.