Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Inverse a

  1. #11
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That works, but is dangerous on large files — your script will quickly run out of memory and die. Rather, a better solution involves a temporary file:
    Code:
    define('BUFSIZ', 512); // Handle half a kilobyte at a time.
    
    // fcopy(resource $src, resource $dest):
    //   copy the contents of $src to $dest.
    // $src must be readable, and $dest writeable.
    function fcopy($src, $dest) {
      for (; !feof($src); $buf = fread($src, BUFSIZ))
        fwrite($dest, $buf);
    }
    
    // file_prepend_contents(string $filename, string $data):
    //   prepend some data to a file.
    function file_prepend_contents($filename, $data) {
      if ($data === '') return;
    
      // Open the provided filename, and a temporary file.
      $f  = fopen($filename, 'r');
      $ft = tmpfile();
    
      // Copy the contents of the file to the temporary file.
      fcopy($f, $ft);
    
      // Seek the temporary file (which is in r+ mode) to the beginning,
      // and re-open the main file for writing, truncating it.
      fclose($f);
      fseek($ft, 0);
      $f = fopen($filename, 'w');
    
      // Write the data to the main file, followed by the original contents.
      fwrite($f, $data);
      fcopy($ft, $f);
    
      // Close the handles.
      fclose($f);
      fclose($ft);
    }
    Last edited by Twey; 12-06-2008 at 02:12 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  2. #12
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Hmmm I tried replacing my code with that but it didn't write to the text file. It brought up the success message but the data wasn't in the text file this is how i put it in...
    PHP Code:
    $File "questions.txt";
    $Handle fopen($File"r+");
    $Data "<p>From:" $visitor .  "<br />Submitted on:" $todayis "<br />" $notes "</p><hr />";
    define('BUFSIZ'512); // Handle half a kilobyte at a time.
    // fcopy(resource $src, resource $dest):
    //   copy the contents of $src to $dest.
    // $src must be readable, and $dest writeable.
    function fcopy($src$dest) {
      for (; !
    feof($src); $buf fread($srcBUFSIZ))
        
    fwrite($dest$buf);
    }

    // file_prepend_contents(string $filename, string $data):
    //   prepend some data to a file.
    function file_prepend_contents($File$data) {
      if (
    $data === '') return;

      
    // Open the provided filename, and a temporary file.
      
    $f  fopen($File'r');
      
    $ft tmpfile();

      
    // Copy the contents of the file to the temporary file.
      
    fcopy($f$ft);

      
    // Seek the temporary file (which is in r+ mode) to the beginning,
      // and re-open the main file for writing, truncating it.
      
    fclose($f);
      
    fseek($ft0);
      
    $f fopen($File'w');

      
    // Write the data to the main file, followed by the original contents.
      
    fwrite($f$data);
      
    fcopy($ft$f);

      
    // Close the handles.
      
    fclose($f);
      
    fclose($ft);
    }
    ?> 

  3. #13
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Are you familiar with the concept of a function? If not, you should probably go through a programming language tutorial.

    In a nutshell: a function is a reusable block of code that you can call with your own variables as parameters. There is no need to substitute in your variable, but you must call the function — it won't do anything without being called.

    By convention, variables do not start with a capital letter.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #14
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I had not heard of case sensitive variables. Is $File different from $file? I've used functions before but not with php. I haven't done a lot with php...

    So I should leave my variables as they were declared, paste your original code in, as it was under my variables, and it should write to the test file?

    Thanks for your help.

  5. #15
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    In PHP, $File is the same as $file is the same as $FILE (this being the meaning of 'case-insensitive'), but by convention it would be written $file.

    It is a function: it won't do anything until you call it, passing in the appropriate values. Please, read a programming language tutorial.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •