Jump to content

Archived

This topic is now archived and is closed to further replies.

Ninjakreborn

working with .php files.

Recommended Posts

Ok. say I have a file.
I need to take that file, work with it, and save it, as another copy using php.

I use get_file_contents, or file open to get the file open, then I can make my changes, then I Have to resave it, is this with file copy or something?

Share this post


Link to post
Share on other sites
does this help?

$infile = fopen("myfile.txt", "r") // as readonly
while (!feof($infile))
{
$buffer = fgets($infile, 2048); // or $buffer = fread($infile.....
}
fclose($infile);

// do whatever you want

$outfile = fopen("mynewfile.txt", "w") // open writeable
fwrite($outfile, $buffer)
fclose ($outfile);

cheers,
tdw

Share this post


Link to post
Share on other sites
[code]$infile = fopen("myfile.txt", "r") // as readonly
while (!feof($infile))
{
$buffer = fgets($infile, 2048); // or $buffer = fread($infile.....
}
fclose($infile);

// do whatever you want

$outfile = fopen("mynewfile.txt", "w") // open writeable
fwrite($outfile, $buffer)
fclose ($outfile);[/code]
Now to break this down.
So the open, that is opening the file as read only.  I got that part.
Then
[code]while (!feof($infile))
{
$buffer = fgets($infile, 2048); // or $buffer = fread($infile.....
}[/code]
I can look up end of file function on php.net, but what would the difference be in fgets and file_get_contents
Which would be better Also what is the 2048,
I appreciate the help so far, thanks.
It looks like the top part opens the file, saves the info in a variable.
then closes the file
then it attempts to oepn another file, if one doesn't exist then obviously it creates it, then it writes that information to it, and that's it right.

Share this post


Link to post
Share on other sites
hi

well [color=blue]fread[/color] reads a total amount up to length, and [color=blue]fgets[/color] reads it line by line

so if you want to read it all in one go you can use
[code]$buffer = fread($infile, filesize("myfile.txt")); // or use a variable for the filename[/code]

or to only read a portion of it you can specify the number of bytes to read: (2kbs in this example)
[code]$buffer = fread($infile, 2048); // or use a variable for the filename[/code]

or to read the file line by line so you can deal with each line while reading it, you use fgets and specify the number of bytes to read (in that line): (2kbs in this example)
[code]$buffer = fgets($infile, 2048); // or use a variable for the filename[/code]

i dont know about [color=blue]get_file_contents[/color]

and then yes, the "w" in fopen means will attempt to create it, so check your permissions in the folder

[code]$outfile = fopen("mynewfile.txt", "w")[/code]

cheers,
tdw

Share this post


Link to post
Share on other sites
I will play with this, I have to come up with something rather wierd, it has to be a cms added into each one of those pages, but now I can build the framework for the filehandling, and go from there, thanks

Share this post


Link to post
Share on other sites
Well I did some modifications, I am going to have to instead carry all of this information over into paypal's IPN, and then do all this stuff there I think.  I will have to store the text, and template they choose into a session, and let the stuff that comes back from paypal deal with it.  I can only hope that when paypal sends the ipn back to the script that I can have the sessions available to do what I need.  If not, I can pass it all through paypal, I think that would be the best time to do all of this, so I can have a full system built.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.