Php question

Reply

Join Date: Nov 2007
Posts: 68
Reputation: jencinas69 is an unknown quantity at this point 
Solved Threads: 1
jencinas69's Avatar
jencinas69 jencinas69 is offline Offline
Junior Poster in Training

Php question

 
0
  #1
Aug 26th, 2008
Hello I have a txt file with this content

456510 P115 JANDAY CONURE Very pretty, green/orange/blue, but keeps telling ME to shut up! $250/obo. 928-555-1212.


I will like to now if its possible to write

ID Category Description in top of the file like this

ID Category Description
456510 P115 JANDAY CONURE Very pretty, green/orange/blue, but keeps telling ME to shut up! $250/obo. 928-555-1212.


Thank you
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Php question

 
0
  #2
Aug 26th, 2008
Originally Posted by jencinas69 View Post
Hello I have a txt file with this content

456510 P115 JANDAY CONURE Very pretty, green/orange/blue, but keeps telling ME to shut up! $250/obo. 928-555-1212.


I will like to now if its possible to write

ID Category Description in top of the file like this

ID Category Description
456510 P115 JANDAY CONURE Very pretty, green/orange/blue, but keeps telling ME to shut up! $250/obo. 928-555-1212.


Thank you
found this at php.net
Originally Posted by php.net
I needed to append, but I needed to write on the file's beginning, and after some hours of effort this worked for me:
  1. $file = "file.txt";
  2. if (!file_exists("file.txt")) touch("file.txt");
  3. $fh = fopen("file.txt", "r");
  4. $fcontent = fread($fh, filesize("file.txt"));
  5.  
  6. $towrite = "$newcontent $fcontent";
  7.  
  8. $fh22 = fopen('file.txt', 'w+');
  9. fwrite($fh2, $towrite);
  10. fclose($fh);
  11. fclose($fh2);
Last edited by R0bb0b; Aug 26th, 2008 at 3:45 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 68
Reputation: jencinas69 is an unknown quantity at this point 
Solved Threads: 1
jencinas69's Avatar
jencinas69 jencinas69 is offline Offline
Junior Poster in Training

Re: Php question

 
0
  #3
Aug 26th, 2008
Using this code :

  1.  
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. ini_set("display_errors", true);
  4.  
  5.  
  6. $file = "petsfeed.txt";
  7.  
  8. if (!file_exists("petsfeed.txt")) touch("petsfeed.txt");
  9.  
  10. $fh = fopen("petsfeed.txt", "r");
  11.  
  12. $fcontent = fread($fh, filesize("petsfeed.txt"));
  13.  
  14. $towrite = "ID Category Description";
  15.  
  16.  
  17.  
  18. $fh22 = fopen('file.txt', 'w+');
  19.  
  20. fwrite($fh2, $towrite);
  21.  
  22. fclose($fh);
  23.  
  24. fclose($fh2);

I get this error


Warning: fwrite(): supplied argument is not a valid stream resource in /var/www/html/yumasun/media/gadzoo/txt.php on line 21

Warning: fclose(): supplied argument is not a valid stream resource in /var/www/html/yumasun/media/gadzoo/txt.php on line 25
Last edited by peter_budo; Aug 27th, 2008 at 12:44 pm. Reason: Closing tag is [/code] not [\code]
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: Php question

 
0
  #4
Aug 26th, 2008
If you're using PHP 5, you can do the same type of thing like this...
  1. <?php
  2. $file_path = 'path_to/my_file.txt';
  3.  
  4. if ( is_file( $file_path ) ) {
  5. $contents = file_get_contents( $file_path );
  6. $modified = "ID Category Description\n$contents";
  7. file_put_contents( $file_path, modified );
  8. } else {
  9. die( "File does not exist at this location: $file_path" );
  10. }
  11. ?>
Please change $file_path value to the path to your specific file...

Hope this helps.
Google is the answer to all of your questions -- the trick is knowing what question to ask in your specific predicament.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 68
Reputation: jencinas69 is an unknown quantity at this point 
Solved Threads: 1
jencinas69's Avatar
jencinas69 jencinas69 is offline Offline
Junior Poster in Training

Re: Php question

 
0
  #5
Aug 26th, 2008
Not working is not writing to petsfeed.txt
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: Php question

 
0
  #6
Aug 26th, 2008
Is petsfeed.txt on a server? Check it's read-write permissions.
Are you getting any error message, if so, please post it here.
  1. <?php
  2. $file_path = 'path_to/my_file.txt';
  3.  
  4. if ( is_file( $file_path ) ) {
  5. if ( is_readable( $file_path ) && is_writable( $file_path ) ) {
  6. $contents = file_get_contents( $file_path );
  7. $modified = "ID Category Description\n$contents";
  8. file_put_contents( $file_path, modified );
  9. } else {
  10. die( "Please change the read-write permissions of this file: $file_path" );
  11. }
  12. } else {
  13. die( "File does not exist at this location: $file_path" );
  14. }
  15. ?>
Google is the answer to all of your questions -- the trick is knowing what question to ask in your specific predicament.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 68
Reputation: jencinas69 is an unknown quantity at this point 
Solved Threads: 1
jencinas69's Avatar
jencinas69 jencinas69 is offline Offline
Junior Poster in Training

Re: Php question

 
0
  #7
Aug 27th, 2008
Originally Posted by langsor View Post
Is petsfeed.txt on a server? Check it's read-write permissions.
Are you getting any error message, if so, please post it here.
  1. <?php
  2. $file_path = 'path_to/my_file.txt';
  3.  
  4. if ( is_file( $file_path ) ) {
  5. if ( is_readable( $file_path ) && is_writable( $file_path ) ) {
  6. $contents = file_get_contents( $file_path );
  7. $modified = "ID Category Description\n$contents";
  8. file_put_contents( $file_path, modified );
  9. } else {
  10. die( "Please change the read-write permissions of this file: $file_path" );
  11. }
  12. } else {
  13. die( "File does not exist at this location: $file_path" );
  14. }
  15. ?>

It is on a server and no is not giving me any error message is just not writting anything to the file
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Php question

 
0
  #8
Aug 27th, 2008
Originally Posted by jencinas69 View Post
Using this code :

  1.  
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. ini_set("display_errors", true);
  4.  
  5.  
  6. $file = "petsfeed.txt";
  7.  
  8. if (!file_exists("petsfeed.txt")) touch("petsfeed.txt");
  9.  
  10. $fh = fopen("petsfeed.txt", "r");
  11.  
  12. $fcontent = fread($fh, filesize("petsfeed.txt"));
  13.  
  14. $towrite = "ID Category Description";
  15.  
  16.  
  17.  
  18. $fh22 = fopen('file.txt', 'w+');
  19.  
  20. fwrite($fh2, $towrite);
  21.  
  22. fclose($fh);
  23.  
  24. fclose($fh2);

I get this error


Warning: fwrite(): supplied argument is not a valid stream resource in /var/www/html/yumasun/media/gadzoo/txt.php on line 21

Warning: fclose(): supplied argument is not a valid stream resource in /var/www/html/yumasun/media/gadzoo/txt.php on line 25
Sorry, that was a bad example
on this line
$fh22 = fopen('file.txt', 'w+'); is using the variable $fh22 and the next two lines need to use that variable instead of $fh2
write($fh2, $towrite); fclose($fh2);
Sometimes what other people post is not always correct, even(especially) on php.net.
Last edited by R0bb0b; Aug 27th, 2008 at 5:47 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: Php question

 
0
  #9
Aug 27th, 2008
Hmmn, maybe the error message is not going to the browser but going to a log or turned off.

What version of PHP is on the server, because my script example requires PHP 5 or newer, but if PHP could not find the functions I'm using it would put out an error ...

How are you calling this PHP script. Are you loading it in the browser location bar? If so it should work.

I would post a file that you know will give you an error message and load that in the browser to see that you're getting error messages.

error.php
  1. <?php
  2. require 'foobar';
  3. ?>
should give you this error message:

Warning: require(foobar) [function.require]: failed to open stream: No such file or directory in C:\Documents and Settings\Creative\My Documents\Server\public\admin.loomba\error.php on line 3

Fatal error: require() [function.require]: Failed opening required 'foobar' (include_path='.;..;../..;../../..') in C:\Documents and Settings\Creative\My Documents\Server\public\admin.loomba\error.php on line 3


If you have access to your php.ini file you can search for 'error' and see what the settings are too.

Good luck
Google is the answer to all of your questions -- the trick is knowing what question to ask in your specific predicament.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 68
Reputation: jencinas69 is an unknown quantity at this point 
Solved Threads: 1
jencinas69's Avatar
jencinas69 jencinas69 is offline Offline
Junior Poster in Training

Re: Php question

 
0
  #10
Aug 27th, 2008
Originally Posted by langsor View Post
Hmmn, maybe the error message is not going to the browser but going to a log or turned off.

What version of PHP is on the server, because my script example requires PHP 5 or newer, but if PHP could not find the functions I'm using it would put out an error ...

How are you calling this PHP script. Are you loading it in the browser location bar? If so it should work.

I would post a file that you know will give you an error message and load that in the browser to see that you're getting error messages.

error.php
  1. <?php
  2. require 'foobar';
  3. ?>
should give you this error message:

Warning: require(foobar) [function.require]: failed to open stream: No such file or directory in C:\Documents and Settings\Creative\My Documents\Server\public\admin.loomba\error.php on line 3

Fatal error: require() [function.require]: Failed opening required 'foobar' (include_path='.;..;../..;../../..') in C:\Documents and Settings\Creative\My Documents\Server\public\admin.loomba\error.php on line 3


If you have access to your php.ini file you can search for 'error' and see what the settings are too.

Good luck
I am loding in it on the browser I am not getting an error but still is not writting

to the file



  1.  
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. ini_set("display_errors", true);
  4.  
  5.  
  6. $file = "petsfeed.txt";
  7.  
  8. if (!file_exists("petsfeed.txt")) touch("petsfeed.txt");
  9.  
  10. $fh = fopen("petsfeed.txt", "r");
  11.  
  12. $fcontent = fread($fh, filesize("petsfeed.txt"));
  13.  
  14. $towrite = "ID Category Description";
  15.  
  16.  
  17.  
  18. $fh22 = fopen('file.txt', 'w+');
  19.  
  20. fwrite($fh22, $towrite);
  21.  
  22. fclose($fh);
  23.  
  24. fclose($fh22);
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum


Views: 1956 | Replies: 28
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC