Re: [PHP] Writing to files
Thanks for the help guys. Jason On Fri, 2003-07-11 at 15:43, David Nicholson wrote: > Hello, > > > This is a reply to an e-mail that you wrote on Fri, 11 Jul 2003 at 20:37, > lines prefixed by '>' were originally written by you. > > Any ideas on how I can print the lines of my file in reverse order, > > then? > > How about... > > $fp = fopen("yourfile.txt","r"); > $filecontents = ""; > while(!feof($fp)){ > $filecontents.=fgets($fp,1024); // read entire file into > $filecontents > } > > $filelines = split("\n",$filecontents); // split file into individual > lines > > for($i=(count($filelines)-1);$i>=0;$i--){ // loop through array in > reverse order > echo $filelines[$i] . "\n"; > } > > -- > phpmachine :: The quick and easy to use service providing you with > professionally developed PHP scripts :: http://www.phpmachine.com/ > > Professional Web Development by David Nicholson > http://www.djnicholson.com/ > > QuizSender.com - How well do your friends actually know you? > http://www.quizsender.com/ > (developed entirely in PHP) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Writing to files
Hello, This is a reply to an e-mail that you wrote on Fri, 11 Jul 2003 at 20:37, lines prefixed by '>' were originally written by you. > Any ideas on how I can print the lines of my file in reverse order, > then? How about... $fp = fopen("yourfile.txt","r"); $filecontents = ""; while(!feof($fp)){ $filecontents.=fgets($fp,1024); // read entire file into $filecontents } $filelines = split("\n",$filecontents); // split file into individual lines for($i=(count($filelines)-1);$i>=0;$i--){ // loop through array in reverse order echo $filelines[$i] . "\n"; } -- phpmachine :: The quick and easy to use service providing you with professionally developed PHP scripts :: http://www.phpmachine.com/ Professional Web Development by David Nicholson http://www.djnicholson.com/ QuizSender.com - How well do your friends actually know you? http://www.quizsender.com/ (developed entirely in PHP) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Writing to files
You could try using the file() function. Then loop backward through the array or use array_reverse. -- Rob "Jason Giangrande" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Any ideas on how I can print the lines of my file in reverse order, > then? Does fgets() always process from the beginning of the file even > if you open the file with the pointer at the end? I tried to get the > line count of the file and go through each line of the file backwards > but that doesn't seem to work, so either it's impossible or I'm going > about it the wrong way. Here's the code: > > $fh = fopen("data.txt", "a+") or die("Could not open file"); > $line_num = 0; > while (! feof($fh)) { > if ($line = fgets($fh, 1048576)) { > $line_num++; > } > } > while ($line_num != 0)) { > if ($line = fgets($fh, 1048576)) { > print $line; > $line_num--; > } > } > > Thanks, > Jason > > > On Fri, 2003-07-11 at 15:04, David Nicholson wrote: > > Hello, > > > > > > This is a reply to an e-mail that you wrote on Fri, 11 Jul 2003 at 19:56, > > lines prefixed by '>' were originally written by you. > > > Is there a way to write to a beginning of a file without it > > > overwriting > > > data that's already there or do I have to write to the end of the file > > > in order to preserve data? I ask because it would be much easier to > > > print the lines of the file out in order of last added first if I > > > could > > > add lines at the top of the file. > > > > Not without reading the entire file into a variable first then appending > > that variable to the data you wish to add and saving the entire file again > > (which will obviously take longer than appending to the end of the file). > > > > David. > > > > -- > > phpmachine :: The quick and easy to use service providing you with > > professionally developed PHP scripts :: http://www.phpmachine.com/ > > > > Professional Web Development by David Nicholson > > http://www.djnicholson.com/ > > > > QuizSender.com - How well do your friends actually know you? > > http://www.quizsender.com/ > > (developed entirely in PHP) > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Writing to files
> Any ideas on how I can print the lines of my file in reverse order, > then? Does fgets() always process from the beginning of the file even > if you open the file with the pointer at the end? I tried to get the > line count of the file and go through each line of the file backwards > but that doesn't seem to work, so either it's impossible or I'm going > about it the wrong way. Here's the code: > > $fh = fopen("data.txt", "a+") or die("Could not open file"); > $line_num = 0; > while (! feof($fh)) { > if ($line = fgets($fh, 1048576)) { > $line_num++; > } > } > while ($line_num != 0)) { > if ($line = fgets($fh, 1048576)) { > print $line; > $line_num--; > } > } $file = file('data.txt'); $rev_file = array_reverse($file); foreach($rev_file as $line) { echo $line; } ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Writing to files
Any ideas on how I can print the lines of my file in reverse order, then? Does fgets() always process from the beginning of the file even if you open the file with the pointer at the end? I tried to get the line count of the file and go through each line of the file backwards but that doesn't seem to work, so either it's impossible or I'm going about it the wrong way. Here's the code: $fh = fopen("data.txt", "a+") or die("Could not open file"); $line_num = 0; while (! feof($fh)) { if ($line = fgets($fh, 1048576)) { $line_num++; } } while ($line_num != 0)) { if ($line = fgets($fh, 1048576)) { print $line; $line_num--; } } Thanks, Jason On Fri, 2003-07-11 at 15:04, David Nicholson wrote: > Hello, > > > This is a reply to an e-mail that you wrote on Fri, 11 Jul 2003 at 19:56, > lines prefixed by '>' were originally written by you. > > Is there a way to write to a beginning of a file without it > > overwriting > > data that's already there or do I have to write to the end of the file > > in order to preserve data? I ask because it would be much easier to > > print the lines of the file out in order of last added first if I > > could > > add lines at the top of the file. > > Not without reading the entire file into a variable first then appending > that variable to the data you wish to add and saving the entire file again > (which will obviously take longer than appending to the end of the file). > > David. > > -- > phpmachine :: The quick and easy to use service providing you with > professionally developed PHP scripts :: http://www.phpmachine.com/ > > Professional Web Development by David Nicholson > http://www.djnicholson.com/ > > QuizSender.com - How well do your friends actually know you? > http://www.quizsender.com/ > (developed entirely in PHP) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Writing to files
Hello, This is a reply to an e-mail that you wrote on Fri, 11 Jul 2003 at 19:56, lines prefixed by '>' were originally written by you. > Is there a way to write to a beginning of a file without it > overwriting > data that's already there or do I have to write to the end of the file > in order to preserve data? I ask because it would be much easier to > print the lines of the file out in order of last added first if I > could > add lines at the top of the file. Not without reading the entire file into a variable first then appending that variable to the data you wish to add and saving the entire file again (which will obviously take longer than appending to the end of the file). David. -- phpmachine :: The quick and easy to use service providing you with professionally developed PHP scripts :: http://www.phpmachine.com/ Professional Web Development by David Nicholson http://www.djnicholson.com/ QuizSender.com - How well do your friends actually know you? http://www.quizsender.com/ (developed entirely in PHP) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] writing to files
> Looks like you're using fopen() incorrectly. Try: > fopen("$file_name","r+"); How about just fopen($file_name, "r+"); instead. No need for the surrounding "s. -- JR -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Writing to Files
try creating a script with fopen(), fwrite(), etc. When you run into problems, ask here. $filex = fopen("myfile","w"); fwrite( $filex, "write this here"); fclose($filex); -Original Message- From: Sebastian A. [mailto:[EMAIL PROTECTED]] Sent: Friday, April 05, 2002 10:29 AM To: PHP General List (PHP.NET) Subject: [PHP] Writing to Files Hey All, I have recently been trying to create some logs for the install script I have been making (to make it easier for me to diagnose problems) but I am wondering how to create and write to text files. I know about the fopen() and fwrite() functions, which theoretically should enable me to do this however I am not exactly sure how I should go about this. Also, I realize that there is limited formatting I can have with text, however I would like to know if its possible to control the line spacing between the entries, and whether or not its possible to indent lines. Lastly, are text files a good idea for logs? Do any of you use anything else I should be aware of? Thanks for all your help! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Writing to Files
http://www.php.net/manual/en/function.printf.php try sprintf to format the string and then fwrite I use text files to log debug msgs in my programs as they are not too heavy duty applications. - Original Message - From: "Sebastian A." <[EMAIL PROTECTED]> To: "PHP General List (PHP.NET)" <[EMAIL PROTECTED]> Sent: Friday, April 05, 2002 11:29 AM Subject: [PHP] Writing to Files > Hey All, > I have recently been trying to create some logs for the install script I > have been making (to make it easier for me to diagnose problems) but I am > wondering how to create and write to text files. I know about the fopen() > and fwrite() functions, which theoretically should enable me to do this > however I am not exactly sure how I should go about this. Also, I realize > that there is limited formatting I can have with text, however I would like > to know if its possible to control the line spacing between the entries, and > whether or not its possible to indent lines. Lastly, are text files a good > idea for logs? Do any of you use anything else I should be aware of? > > Thanks for all your help! > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Writing to files
There is no need to use cookies, sessions, write to a file or somethings like that... just pass the vars from a page to another through hidden fields in the next page form. Eg. William El mié, 27-02-2002 a las 02:18, Chris Kay escribió: > > Question I have is, Anyway know of a better way to store temp > information? > > I have a problem that a script I use, uses many pages and after each > page the information from the form > Is stored and the next page is shown ect > > It uses more than 20 variables so I can not store the data in cookies. > I could store the data in a temp file created but problem is that the > webserver would need to create > The file which is a security risk (I would like to find another way > other than this). > I don't really want to use sessions but if it's the last resort I guess > I will have to. > > Other than the above any one have a better soluition? > > Running php 4.1.1 on RH7.2 > > --- > Chris Kay, Eleet Internet Services > [EMAIL PROTECTED] > --- _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Writing to files
Not sure I understand how you concluded that web server should not create files, it certainly should not be able to create files on client machines without permission of user (which it cannot do). Users should not be able to upload files to the server without the permission of the web application, but if a web application performs the upload and controls where the file goes and what is in the file, that would be safe. If the web application completely controls where temporary files go and what goes in them (variables) then how is that unsafe? On the subject of using the database, check out which environment variables that actually control session management (you can do this by running the phpinfo() function. There are two major configuration options which you have full control over; 1. If the sys admin controls the "default" configuration of where to write the session data (usually points to /tmp directory), then the session_set_save_handler function is your way to override those parameters. 2. In fact if you read the routine that performs garbage collection (deletes expired session data) session management will pass to that routine the parameter that controls the default lifetime from the sysadmin's setup data, which you as the writer of the routine are free to ignore and use your own. Only caveat is you need to include these routines in every page that records data to a session (no big deal). I wish you luck on this, you seem to have been advised on some rules, by people who didn't know what they were talking about, or perhaps you understood their recommendations out of context, and that makes it hard for you. I really think session management is the way to resolve your problem. It would be interesting for you to get a couple of opinions on the subject of creating files on a server. You could create routines that would write to physical files in these save_handler functions and put them in your user directory if you like, but I would not mix these files in the same directory as your application code, perhaps a sub directory. I would be willing to bet there are log files that are updated on your server every time someone accesses one of your web pages. Good luck, Warren Vail -Original Message- From: Chris Kay [mailto:[EMAIL PROTECTED]] Sent: Wednesday, February 27, 2002 12:36 AM To: 'Warren Vail' Cc: [EMAIL PROTECTED] Subject:RE: [PHP] Writing to files I know I can do this with sessions, reason I am asking is webserver should not be able to create file (for security reasons), I would of maybe thought php could create a file as a different user. php is not always used by the box owner. I find it strange that such a option Is only available if you run the box or the webserver is run as that user. Sessions and such need configurations that need to be configured by the server admin Ect I was hoping there was a way that didn't rely on a special configuration And stayed in the users directory --- Chris Kay, Eleet Internet Services [EMAIL PROTECTED] --- -Original Message- From: Warren Vail [mailto:[EMAIL PROTECTED]] Sent: Wednesday, 27 February 2002 6:51 PM To: Chris Kay Subject: RE: [PHP] Writing to files Suggest you understand how session management works. Whether you store in a file or database, the entries are removed if the user fails to return after a limit amount of time, and are kept in a separate table in the database. You can post the results to your actual data tables when you are completed. If you don't want to store the data in files, or in a database, what did you have in mind? There is no rule that says you can only store permanent data in a data base. -Original Message- From: Chris Kay [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 26, 2002 11:43 PM To: 'Warren Vail'; [EMAIL PROTECTED] Subject:RE: [PHP] Writing to files The data will be stored in mysql, but I don't wish to store in sql untill its completed. In case a user leave the application before completing it. --- Chris Kay, Eleet Internet Services [EMAIL PROTECTED] --- -Original Message- From: Warren Vail [mailto:[EMAIL PROTECTED]] Sent: Wednesday, 27 February 2002 6:37 PM To: Chris Kay; [EMAIL PROTECTED] Subject: RE: [PHP] Writing to files What you are describing is exactly how session management works, storing things in a file in the /tmp directory. Perhaps you could consider using the session save handler functions to store the session data in your protected database (MySQL?). Warren Vail -Original Message- From: Chris Kay [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 26, 2002 11:19 PM To: [EMAIL PROTECTED] Subject:[PHP] Writing to files Question I have is, Anyway know of a better way to store temp information? I have a problem that a script I use, uses many pages and after each page the information from the form Is stored a
RE: [PHP] Writing to files
I know I can do this with sessions, reason I am asking is webserver should not be able to create file (for security reasons), I would of maybe thought php could create a file as a different user. php is not always used by the box owner. I find it strange that such a option Is only available if you run the box or the webserver is run as that user. Sessions and such need configurations that need to be configured by the server admin Ect I was hoping there was a way that didn't rely on a special configuration And stayed in the users directory --- Chris Kay, Eleet Internet Services [EMAIL PROTECTED] --- -Original Message- From: Warren Vail [mailto:[EMAIL PROTECTED]] Sent: Wednesday, 27 February 2002 6:51 PM To: Chris Kay Subject: RE: [PHP] Writing to files Suggest you understand how session management works. Whether you store in a file or database, the entries are removed if the user fails to return after a limit amount of time, and are kept in a separate table in the database. You can post the results to your actual data tables when you are completed. If you don't want to store the data in files, or in a database, what did you have in mind? There is no rule that says you can only store permanent data in a data base. -Original Message- From: Chris Kay [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 26, 2002 11:43 PM To: 'Warren Vail'; [EMAIL PROTECTED] Subject: RE: [PHP] Writing to files The data will be stored in mysql, but I don't wish to store in sql untill its completed. In case a user leave the application before completing it. --- Chris Kay, Eleet Internet Services [EMAIL PROTECTED] --- -Original Message- From: Warren Vail [mailto:[EMAIL PROTECTED]] Sent: Wednesday, 27 February 2002 6:37 PM To: Chris Kay; [EMAIL PROTECTED] Subject: RE: [PHP] Writing to files What you are describing is exactly how session management works, storing things in a file in the /tmp directory. Perhaps you could consider using the session save handler functions to store the session data in your protected database (MySQL?). Warren Vail -Original Message- From: Chris Kay [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 26, 2002 11:19 PM To: [EMAIL PROTECTED] Subject:[PHP] Writing to files Question I have is, Anyway know of a better way to store temp information? I have a problem that a script I use, uses many pages and after each page the information from the form Is stored and the next page is shown ect It uses more than 20 variables so I can not store the data in cookies. I could store the data in a temp file created but problem is that the webserver would need to create The file which is a security risk (I would like to find another way other than this). I don't really want to use sessions but if it's the last resort I guess I will have to. Other than the above any one have a better soluition? Running php 4.1.1 on RH7.2 --- Chris Kay, Eleet Internet Services [EMAIL PROTECTED] --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Writing to files
The data will be stored in mysql, but I don't wish to store in sql untill its completed. In case a user leave the application before completing it. --- Chris Kay, Eleet Internet Services [EMAIL PROTECTED] --- -Original Message- From: Warren Vail [mailto:[EMAIL PROTECTED]] Sent: Wednesday, 27 February 2002 6:37 PM To: Chris Kay; [EMAIL PROTECTED] Subject: RE: [PHP] Writing to files What you are describing is exactly how session management works, storing things in a file in the /tmp directory. Perhaps you could consider using the session save handler functions to store the session data in your protected database (MySQL?). Warren Vail -Original Message- From: Chris Kay [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 26, 2002 11:19 PM To: [EMAIL PROTECTED] Subject:[PHP] Writing to files Question I have is, Anyway know of a better way to store temp information? I have a problem that a script I use, uses many pages and after each page the information from the form Is stored and the next page is shown ect It uses more than 20 variables so I can not store the data in cookies. I could store the data in a temp file created but problem is that the webserver would need to create The file which is a security risk (I would like to find another way other than this). I don't really want to use sessions but if it's the last resort I guess I will have to. Other than the above any one have a better soluition? Running php 4.1.1 on RH7.2 --- Chris Kay, Eleet Internet Services [EMAIL PROTECTED] --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Writing to files
What you are describing is exactly how session management works, storing things in a file in the /tmp directory. Perhaps you could consider using the session save handler functions to store the session data in your protected database (MySQL?). Warren Vail -Original Message- From: Chris Kay [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 26, 2002 11:19 PM To: [EMAIL PROTECTED] Subject:[PHP] Writing to files Question I have is, Anyway know of a better way to store temp information? I have a problem that a script I use, uses many pages and after each page the information from the form Is stored and the next page is shown ect It uses more than 20 variables so I can not store the data in cookies. I could store the data in a temp file created but problem is that the webserver would need to create The file which is a security risk (I would like to find another way other than this). I don't really want to use sessions but if it's the last resort I guess I will have to. Other than the above any one have a better soluition? Running php 4.1.1 on RH7.2 --- Chris Kay, Eleet Internet Services [EMAIL PROTECTED] --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Writing to files
On Mon, May 14, 2001 at 03:05:30PM +0100, Tarrant Costelloe wrote: > What's the most basic syntax for writing to a text file using PHP. I would > like to store some variables in one and then retrieve them a later stage...? This will append This is pretty basic to the end of the file instead of overwriting the file. It is really easy to read in the entire file into an array. Probably not very efficient, but will get you started quickly. "; } ?> Search the online manual for more information on these functions. > > Taz > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- Jason Stechschulte [EMAIL PROTECTED] -- If I don't document something, it's usually either for a good reason, or a bad reason. In this case it's a good reason. :-) -- Larry Wall in <[EMAIL PROTECTED]> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]