View Full Version : Reading then Writing Info + Read
Jam-ez
06-12-2008, 09:55 PM
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;
$file = fopen("genchat.php", "r+");
$read = fread($file, filesize($filename));
$data = ("$data $read");
fwrite($file, $data);
fclose($file);
Regards,
James.
Protege
06-12-2008, 10:08 PM
You want to open a file, read from it then delete all contents and write to it?
Jam-ez
06-12-2008, 10:30 PM
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?
Protege
06-12-2008, 10:41 PM
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
Jam-ez
06-12-2008, 10:45 PM
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).
$file = fopen("genchat.php", "a+");
$read = fread($file, filesize($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?
Jam-ez
07-12-2008, 11:15 AM
$file = fopen("genchat.php", "a+");
$read = fread($file, filesize($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.
Jam-ez
07-12-2008, 11:41 AM
Can't edit. :(
Anyway, I managed to come up with this code:
if (file_exists("genchat.php"))
{
$file = fopen("genchat.php", "r+");
$filename = "genchat.php";
$read = fread($file, filesize($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($file, filesize($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?
Can't edit. :(
Anyway, I managed to come up with this code:
if (file_exists("genchat.php"))
{
$file = fopen("genchat.php", "r+");
$filename = "genchat.php";
$read = fread($file, filesize($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($file, filesize($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.
Jam-ez
07-12-2008, 01:23 PM
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.
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.
I don't understand what you're trying to say. Writing data over a file keeping the old data? That's not writing over it.
Do you mean add data to the start of the file?
$file = fopen("genchat.php", "r+");
$data = "what you want to be added to the start of the file";
fwrite($file, $data);
fclose($file);
Jam-ez
07-12-2008, 03:38 PM
I don't understand what you're trying to say. Writing data over a file keeping the old data? That's not writing over it.
Do you mean add data to the start of the file?
$file = fopen("genchat.php", "r+");
$data = "what you want to be added to the start of the file";
fwrite($file, $data);
fclose($file);
Would that keep the data already there as well?
Would that keep the data already there as well?
I thought it would, but aparantly not.
Try this:
$filename = "genchat.php";
$data = "stufflololol";
$data .= file_get_contents($filename);
$file = fopen($filename, "w");
fwrite($file, $data);
fclose($file);
Doesn't seem like a very good way to do it, but oh well.
Protege
07-12-2008, 04:57 PM
Would be "r+" if you want to write data at the beginning and keep the rest of the data intack.
If you want to end data to the end of the file "a+"
You can read the data with these flags as well.
Jam-ez
07-12-2008, 05:42 PM
I thought it would, but aparantly not.
Try this:
$filename = "genchat.php";
$data = "stufflololol";
$data .= file_get_contents($filename);
$file = fopen($filename, "w");
fwrite($file, $data);
fclose($file);
Doesn't seem like a very good way to do it, but oh well.
Worked a charm, thanks. :)
Would be "r+" if you want to write data at the beginning and keep the rest of the data intack.
If you want to end data to the end of the file "a+"
You can read the data with these flags as well.
That's what I thought, but r+ overwrites it aswell.
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2026 vBulletin Solutions Inc. All rights reserved.