Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Page 1 of 2 12 LastLast
Results 1 to 10 of 16
  1. #1

    Default Reading then Writing Info + Read

    Hi,

    Basically, in PHP, I want to read a file then overwrite that file with new information and the old read information.

    Any ideas how I'd go about doing this?

    I figured it'd be something like this, but this doesn't work;
    PHP Code:
            $file fopen("genchat.php""r+");
            
    $read fread($filefilesize($filename));
            
    $data = ("$data $read");
            
    fwrite($file$data);
            
    fclose($file); 
    Regards,
    James.

  2. #2
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    You want to open a file, read from it then delete all contents and write to it?
    Hi, names James. I am a web developer.

  3. #3

    Default

    Quote Originally Posted by Protege View Post
    You want to open a file, read from it then delete all contents and write to it?
    Basically, yes. So like this;

    text.html
    wwdadwawd
    fread("text.html")
    $data = ("wd<br />");
    fwrite to text.html $data and what was read on line 1.

    If that makes sense?

  4. #4
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    Well, you got a process of
    1. Open file fopen
    2. Read file fread
    3. Rewrite file fwrite
    4. Close file fclose

    Look at different flags to open files with fopen @ php.net
    Hi, names James. I am a web developer.

  5. #5

    Default

    Quote Originally Posted by Protege View Post
    Well, you got a process of
    1. Open file fopen
    2. Read file fread
    3. Rewrite file fwrite
    4. Close file fclose

    Look at different flags to open files with fopen @ php.net
    I've done that already, but I can't seem to get the fread function to read (in my previous example, it returns nothing when returning $read).

  6. #6
    Join Date
    Jun 2008
    Location
    Manchester
    Posts
    766
    Tokens
    0

    Default

    PHP Code:
    $file fopen("genchat.php""a+");
    $read fread($filefilesize($filename));
    $data "what you want to be added to the end of the file";
    fwrite($file$data);
    fclose($file);
    echo 
    $read
    Is that what you want?

  7. #7

    Default

    Quote Originally Posted by Jxhn View Post
    PHP Code:
    $file fopen("genchat.php""a+");
    $read fread($filefilesize($filename));
    $data "what you want to be added to the end of the file";
    fwrite($file$data);
    fclose($file);
    echo 
    $read
    Is that what you want?
    Thanks for having a go, +rep.

    It was before, but now I need to write to the file what was in the file previously plus the new added data (but with the new added data first).

    Just testing something, will post if I get it.

  8. #8

    Default

    Can't edit.

    Anyway, I managed to come up with this code:
    PHP Code:
        if (file_exists("genchat.php"))
        {
            
    $file fopen("genchat.php""r+");
            
    $filename "genchat.php";
            
    $read fread($filefilesize($filename));
            
    $data = ("$data <br /> $read");
            
    fwrite($file$data);
            
    fclose($file);
        }
        else
        {
            
    $file fopen("genchat.php""a");
            
    $write = ("<head><link href=\"cstyle.css\" rel=\"stylesheet\" type=\"text/css\" /></head>");
            
    fwrite($file$write);
            
    fclose($file);
            
            
    $file fopen("genchat.php""w");
            
    $filename "genchat.php";
            
    $read fread($filefilesize($filename));
            
    $data = ("$data <br /> $read");
            
    fwrite($file$data);
            
    fclose($file);
            
        } 
    Its all good, however there's one problem. It grabs what's already in the file, and puts it BEFORE and AFTER what I'm adding, instead of just after. Any ideas?

  9. #9
    Join Date
    Jun 2008
    Location
    Manchester
    Posts
    766
    Tokens
    0

    Default

    Quote Originally Posted by Jam-ez View Post
    Can't edit.

    Anyway, I managed to come up with this code:
    PHP Code:
        if (file_exists("genchat.php"))
        {
            
    $file fopen("genchat.php""r+");
            
    $filename "genchat.php";
            
    $read fread($filefilesize($filename));
            
    $data = ("$data <br /> $read");
            
    fwrite($file$data);
            
    fclose($file);
        }
        else
        {
            
    $file fopen("genchat.php""a");
            
    $write = ("<head><link href=\"cstyle.css\" rel=\"stylesheet\" type=\"text/css\" /></head>");
            
    fwrite($file$write);
            
    fclose($file);
            
            
    $file fopen("genchat.php""w");
            
    $filename "genchat.php";
            
    $read fread($filefilesize($filename));
            
    $data = ("$data <br /> $read");
            
    fwrite($file$data);
            
    fclose($file);
            
        } 
    Its all good, however there's one problem. It grabs what's already in the file, and puts it BEFORE and AFTER what I'm adding, instead of just after. Any ideas?
    You don't need to fwrite what you've read. The fopen modes do that for you:
    mode Description
    'r' Open for reading only; place the file pointer at the beginning of the file.
    'r+' Open for reading and writing; place the file pointer at the beginning of the file.
    'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
    'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
    'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
    'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
    'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
    'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.

  10. #10

    Default

    Quote Originally Posted by Jxhn View Post
    You don't need to fwrite what you've read. The fopen modes do that for you:
    So what would I do if I wanted to write new data over a file, but keep the old data. You would say append, but that's the end of the file, where as I want the beginning of the file.

Page 1 of 2 12 LastLast

Posting Permissions

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