Is this the most efficient way to get and remove first line in file?

前端 未结 12 2247
天命终不由人
天命终不由人 2020-12-03 03:53

I have a script which, each time is called, gets the first line of a file. Each line is known to be exactly of the same length (32 alphanumeric chars) and terminates with \"

相关标签:
12条回答
  • 2020-12-03 04:09

    you can iterate the file , instead of putting them all in memory

    $handle = fopen("file", "r");
    $first = fgets($handle,2048); #get first line.
    $outfile="temp";
    $o = fopen($outfile,"w");
    while (!feof($handle)) {
        $buffer = fgets($handle,2048);
        fwrite($o,$buffer);
    }
    fclose($handle);
    fclose($o);
    rename($outfile,$file);
    
    0 讨论(0)
  • 2020-12-03 04:10

    I think this is best for any file size

    $myfile = fopen("yourfile.txt", "r") or die("Unable to open file!");
    $ch=1;
    
    while(!feof($myfile)) {
      $dataline= fgets($myfile) . "<br>";
      if($ch == 2){
      echo str_replace(' ', '&nbsp;', $dataline)."\n";
      }
      $ch = 2;
    } 
    fclose($myfile);
    
    0 讨论(0)
  • 2020-12-03 04:13

    No need to create a second temporary file, nor put the whole file in memory:

    if ($handle = fopen("file", "c+")) {             // open the file in reading and editing mode
        if (flock($handle, LOCK_EX)) {               // lock the file, so no one can read or edit this file 
            while (($line = fgets($handle, 4096)) !== FALSE) { 
                if (!isset($write_position)) {        // move the line to previous position, except the first line
                    $write_position = 0;
                } else {
                    $read_position = ftell($handle); // get actual line
                    fseek($handle, $write_position); // move to previous position
                    fputs($handle, $line);           // put actual line in previous position
                    fseek($handle, $read_position);  // return to actual position
                    $write_position += strlen($line);    // set write position to the next loop
                }
            }
            fflush($handle);                         // write any pending change to file
            ftruncate($handle, $write_position);     // drop the repeated last line
            flock($handle, LOCK_UN);                 // unlock the file
        }
        fclose($handle);
    }
    
    0 讨论(0)
  • 2020-12-03 04:14

    You could use file() method.

    Gets the first line

    $content = file('myfile.txt');
    echo $content[0];  
    
    0 讨论(0)
  • 2020-12-03 04:14

    The solutions here didn't work performantly for me. My solution grabs the last line (not the first line, in my case it was not relevant to get the first or last line) from the file and removes that from that file. This is very quickly even with very large files (>150000000 lines).

    function file_pop($file)
    {
        if ($fp = @fopen($file, "c+")) {
            if (!flock($fp, LOCK_EX)) {
                fclose($fp);
            }
            $pos = -1;
            $found = 0;
            while ($found < 2) {
                if (fseek($fp, $pos--, SEEK_END) < 0) { // can not seek to position
                    rewind($fp); // rewind to the beginnung of the file
                    break;
                };
                if (ord(fgetc($fp)) == 10) { // newline
                    $found++;
                }
            }
            $lastpos = ftell($fp); // get current position of file
            $lastline = fgets($fp); // get current line
    
            ftruncate($fp, $lastpos); // truncate file to last position
            flock($fp, LOCK_UN); // unlock
            fclose($fp); // close the file
    
            return trim($lastline);
        }
    }
    
    0 讨论(0)
  • 2020-12-03 04:18

    This will shift the first line of a file, you dont need to load the entire file in memory like you do using the 'file' function. Maybe for small files is a bit more slow than with 'file' (maybe but i bet is not) but is able to manage largest files without problems.

    $firstline = false;
    if($handle = fopen($logFile,'c+')){
        if(!flock($handle,LOCK_EX)){fclose($handle);}
        $offset = 0;
        $len = filesize($logFile);
        while(($line = fgets($handle,4096)) !== false){
            if(!$firstline){$firstline = $line;$offset = strlen($firstline);continue;}
            $pos = ftell($handle);
            fseek($handle,$pos-strlen($line)-$offset);
            fputs($handle,$line);
            fseek($handle,$pos);
        }
        fflush($handle);
        ftruncate($handle,($len-$offset));
        flock($handle,LOCK_UN);
        fclose($handle);
    }
    
    0 讨论(0)
提交回复
热议问题