dougou7008 2014-08-13 19:09
浏览 22
已采纳

搜索文件的单词和删除行

This is a second request on the same subject. I wasn't clear I needed the line to be deleted.

I searched here and found part of a script that is suppose search for a word and delete the line. There seems to be a slight error with what I'm trying to do.

I have an option list in a pull down. I would like for it to remove the line selected. The file choice.php that is called from the pull down page seems to be released when the php below is called called because there is no access denied, or violation errors.

These are the errors I'm getting after adding the 3 last lines I was told I need.

fopen() expects at least 2 parameters, 1 given
implode(): Invalid arguments passed
fwrite() expects parameter 1 to be resource, boolean given
fclose() expects parameter 1 to be resource, boolean given

Thanks in advance

<?php
//  Separate choice.php has the following pull down 
//  Select item to delete from list
//  <option value="item1.php">Item 1</option>
//  <option value="item2.php">Item 2</option>
//  ...... many items.

$workitem = $_POST["itemtodelete"]; 

$file = file("option.list.php");
foreach( $file as $key=>$line ) {
    if( false !== strpos($line, $workitem) ) {
      unset ($file[$key]);
    }
}
// Removed "
"
$file = implode("", $file);
// Told to add this.
$fp = fopen ("option.list.php");
fwrite($fp,implode("",$file);
fclose ($fp);

?>
  • 写回答

2条回答 默认 最新

  • duanche5149 2014-08-13 19:30
    关注

    Ok. You are missing some closing parenthesis, as well as other things.

    $replaceItem = $_POST['itemtodelete']; // You should filter this data
    
    $newContents = "";
    $path = PATH_TO_FILE; // This could be hard coded, but not recommended
    $filename = "option.list.php";
    
    // Check to see if the file exists
    if ( file_exists($path."/".$filename) ) {
        // Wrap our IO stuff so we catch any exceptions
        try {
            // Open the file for reading
            $fp = fopen($path."/".$filename, "r");
            if ($fp) {
                // Loop line-by-line through the file
                while($line = fgets($fp, 4096) !== false) {
    
                    // Only add the line if it doesn't contain $replaceItem
                    // This is case insensitive. I.E. 'item' == 'ITEM'
                    // For case sensitive, use strstr()
                    if ( stristr($line, $replaceItem) == false ) {
                        $newContents .= $line;
                    }
                }
            }
            // Close our file
            fclose($fp);
    
            // Replace the contents of the file with the new contents
            file_put_contents($path."/".$filename, $newContents);                
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }
    

    Edit: Try this. I modified it somewhat.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。