| | |
Php question
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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
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
•
•
•
•
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
•
•
•
•
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:
php Syntax (Toggle Plain Text)
$file = "file.txt"; if (!file_exists("file.txt")) touch("file.txt"); $fh = fopen("file.txt", "r"); $fcontent = fread($fh, filesize("file.txt")); $towrite = "$newcontent $fcontent"; $fh22 = fopen('file.txt', 'w+'); fwrite($fh2, $towrite); fclose($fh); 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.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Using this code :
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
PHP Syntax (Toggle Plain Text)
error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", true); $file = "petsfeed.txt"; if (!file_exists("petsfeed.txt")) touch("petsfeed.txt"); $fh = fopen("petsfeed.txt", "r"); $fcontent = fread($fh, filesize("petsfeed.txt")); $towrite = "ID Category Description"; $fh22 = fopen('file.txt', 'w+'); fwrite($fh2, $towrite); fclose($fh); 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]
•
•
Join Date: Aug 2008
Posts: 381
Reputation:
Solved Threads: 33
If you're using PHP 5, you can do the same type of thing like this...
Please change $file_path value to the path to your specific file...
Hope this helps.
php Syntax (Toggle Plain Text)
<?php $file_path = 'path_to/my_file.txt'; if ( is_file( $file_path ) ) { $contents = file_get_contents( $file_path ); $modified = "ID Category Description\n$contents"; file_put_contents( $file_path, modified ); } else { die( "File does not exist at this location: $file_path" ); } ?>
Hope this helps.
Google is the answer to all of your questions -- the trick is knowing what question to ask in your specific predicament.
•
•
Join Date: Aug 2008
Posts: 381
Reputation:
Solved Threads: 33
Is petsfeed.txt on a server? Check it's read-write permissions.
Are you getting any error message, if so, please post it here.
Are you getting any error message, if so, please post it here.
php Syntax (Toggle Plain Text)
<?php $file_path = 'path_to/my_file.txt'; if ( is_file( $file_path ) ) { if ( is_readable( $file_path ) && is_writable( $file_path ) ) { $contents = file_get_contents( $file_path ); $modified = "ID Category Description\n$contents"; file_put_contents( $file_path, modified ); } else { die( "Please change the read-write permissions of this file: $file_path" ); } } else { die( "File does not exist at this location: $file_path" ); } ?>
Google is the answer to all of your questions -- the trick is knowing what question to ask in your specific predicament.
•
•
•
•
Is petsfeed.txt on a server? Check it's read-write permissions.
Are you getting any error message, if so, please post it here.
php Syntax (Toggle Plain Text)
<?php $file_path = 'path_to/my_file.txt'; if ( is_file( $file_path ) ) { if ( is_readable( $file_path ) && is_writable( $file_path ) ) { $contents = file_get_contents( $file_path ); $modified = "ID Category Description\n$contents"; file_put_contents( $file_path, modified ); } else { die( "Please change the read-write permissions of this file: $file_path" ); } } else { die( "File does not exist at this location: $file_path" ); } ?>
It is on a server and no is not giving me any error message is just not writting anything to the file
•
•
•
•
Using this code :
PHP Syntax (Toggle Plain Text)
error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", true); $file = "petsfeed.txt"; if (!file_exists("petsfeed.txt")) touch("petsfeed.txt"); $fh = fopen("petsfeed.txt", "r"); $fcontent = fread($fh, filesize("petsfeed.txt")); $towrite = "ID Category Description"; $fh22 = fopen('file.txt', 'w+'); fwrite($fh2, $towrite); fclose($fh); 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
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 $fh2write($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.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
•
•
Join Date: Aug 2008
Posts: 381
Reputation:
Solved Threads: 33
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
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
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
PHP Syntax (Toggle Plain Text)
<?php require 'foobar'; ?>
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.
•
•
•
•
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
should give you this error message:PHP Syntax (Toggle Plain Text)
<?php require 'foobar'; ?>
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
to the file
PHP Syntax (Toggle Plain Text)
error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", true); $file = "petsfeed.txt"; if (!file_exists("petsfeed.txt")) touch("petsfeed.txt"); $fh = fopen("petsfeed.txt", "r"); $fcontent = fread($fh, filesize("petsfeed.txt")); $towrite = "ID Category Description"; $fh22 = fopen('file.txt', 'w+'); fwrite($fh22, $towrite); fclose($fh); fclose($fh22);
![]() |
Similar Threads
- PHP question regarding Forms (PHP)
- Integrating Php question (PHP)
- PHP question i think! (Site Layout and Usability)
- A php question (PHP)
Other Threads in the PHP Forum
- Previous Thread: Pagination help. Desperate
- Next Thread: Newbie PHP / MySQL Question
Views: 1956 | Replies: 28
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery js limit link login loop mail mediawiki menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql stored structure subdomain syntax system table tutorial update updates upload url validation validator variable video web xml youtube





