SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I create a flatfile using PHP?

    Im trying to find the code for creating ascii flatfiles using PHP..... I know this will probably be different on win/*nix platforms but Id appreciate any pointers.......
    THE INSTRUCTIONS BELOW ARE OLD AND MAY BE INACCURATE.
    THIS INSTALL METHOD IS NOT RECOMMENDED, IT MAY RUN
    OVER YOUR DOG. <-- MediaWiki installation guide

  2. #2
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not different on windows and *nix systems. All you need to do is use PHP's file handling commands.

    The normal method for storing data in a flat file is to have one "record" per line of the file. This means you have to remove all newlines from the data before saving it - the easiest way to do this it to replace them with <br> tags using a replacement expression.

    When you save the data to the file you should use a common seperator to seperate bits of information. For example lets say you are saving news stories. A suggestion for a file format could be:

    headline<:>timestamp<:>author<:>story

    i.e:

    First News Story<:>990895569<:>Skunk<:>This is the first news story in the text file.<br>It is stored all on one line.

    An easy way to create the above line is to have an array with all of the bits of information in, and then use the PHP implode function to stick it together in one string.

    You can then retrieve all of the news stories in the file by reading them in a line at a time (the file() function can do this) and then using the explode() function to split them into an array by the <:>delimiter.

    Hope that's given you a few pointers,

    Skunk

  3. #3
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but that isnt what I was actually asking...... What I was looking for was a way of creating a file, where no file existed previously.

    When I say "flatfile" Im just referring to a bog standard ascii file, it could be a .txt file for example.
    THE INSTRUCTIONS BELOW ARE OLD AND MAY BE INACCURATE.
    THIS INSTALL METHOD IS NOT RECOMMENDED, IT MAY RUN
    OVER YOUR DOG. <-- MediaWiki installation guide

  4. #4
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh right

    OK - that's easy. Check this out:

    http://www.php.net/manual/en/function.fopen.php
    http://www.php.net/manual/en/function.fwrite.php

    Example code:
    PHP Code:
    $filename "textfile.txt";
    $text "this text will be written to the file";

    $fp fopen$filename,"w+"); 
    fwrite$fp$text); 
    fclose$fp ); 

  5. #5
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats more like it.


    Cheers.
    THE INSTRUCTIONS BELOW ARE OLD AND MAY BE INACCURATE.
    THIS INSTALL METHOD IS NOT RECOMMENDED, IT MAY RUN
    OVER YOUR DOG. <-- MediaWiki installation guide


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
  •