Ninjakreborn Posted October 3, 2006 Share Posted October 3, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/22860-working-with-php-files/ Share on other sites More sharing options...
thedarkwinter Posted October 3, 2006 Share Posted October 3, 2006 does this help?$infile = fopen("myfile.txt", "r") // as readonlywhile (!feof($infile)){ $buffer = fgets($infile, 2048); // or $buffer = fread($infile.....}fclose($infile);// do whatever you want$outfile = fopen("mynewfile.txt", "w") // open writeablefwrite($outfile, $buffer)fclose ($outfile);cheers,tdw Quote Link to comment https://forums.phpfreaks.com/topic/22860-working-with-php-files/#findComment-103018 Share on other sites More sharing options...
Ninjakreborn Posted October 3, 2006 Author Share Posted October 3, 2006 [code]$infile = fopen("myfile.txt", "r") // as readonlywhile (!feof($infile)){ $buffer = fgets($infile, 2048); // or $buffer = fread($infile.....}fclose($infile);// do whatever you want$outfile = fopen("mynewfile.txt", "w") // open writeablefwrite($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_contentsWhich 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 filethen 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. Quote Link to comment https://forums.phpfreaks.com/topic/22860-working-with-php-files/#findComment-103035 Share on other sites More sharing options...
thedarkwinter Posted October 3, 2006 Share Posted October 3, 2006 hiwell [color=blue]fread[/color] reads a total amount up to length, and [color=blue]fgets[/color] reads it line by lineso 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 Quote Link to comment https://forums.phpfreaks.com/topic/22860-working-with-php-files/#findComment-103044 Share on other sites More sharing options...
Ninjakreborn Posted October 3, 2006 Author Share Posted October 3, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/22860-working-with-php-files/#findComment-103046 Share on other sites More sharing options...
Ninjakreborn Posted October 3, 2006 Author Share Posted October 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22860-working-with-php-files/#findComment-103053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.