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

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

I needed to append, but I needed to write on the file's beginning, and after some hours of effort this worked for me:

$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);

Using this code :

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

Member Avatar for langsor

If you're using PHP 5, you can do the same type of thing like this...

<?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" );
}
?>

Please change $file_path value to the path to your specific file...

Hope this helps.

Not working is not writing to petsfeed.txt

Member Avatar for langsor

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
$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" );
}
?>

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
$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 :

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

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.

Member Avatar for langsor

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

<?php
require 'foobar';
?>

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

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

<?php
require 'foobar';
?>

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

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);

the script is writting a file to my server called file.txt with ID Category Description

and I want to thr script tpo write ID Category Description

to the top of petsfeed.txt

the script is writting a file to my server called file.txt with ID Category Description

and I want to thr script tpo write ID Category Description

to the top of petsfeed.txt

Made some adjustments, this works fine for me.

<?
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('petsfeed.txt', 'w+');

fwrite($fh22, $towrite . $fcontent);

fclose($fh);

fclose($fh22);
?>

You will have to use "\n" for new lines of course as this is just writing to one line.

I am getting this errors


Warning: fopen(petsfeed.txt) [function.fopen]: failed to open stream: Permission denied in /var/www/html/yumasun/media/gadzoo/txt.php on line 19

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

with the latest code you send

Warning: fopen(petsfeed.txt) [function.fopen]: failed to open stream: Permission denied in /var/www/html/yumasun/media/gadzoo/txt.php on line 19

Fix the permissions on that file and you should be fine.

Fix the permissions on that file and you should be fine.[/QUOTE

OK thank you

Fix the permissions on that file and you should be fine.[/QUOTE

OK thank you

This works now,

Is it possible to change the name of the file to this format pets_feed_20080101.txt

the date has to change everyday

Thank you

This works now,

Is it possible to change the name of the file to this format pets_feed_20080101.txt

the date has to change everyday

Thank you

Yes, the way the script works is if the file does not exist, it will be created automatically.

Yes, the way the script works is if the file does not exist, it will be created automatically.

Yes but how will it write the name of the file in this format pets_feed_20080101.txt

and change the date to the currwent date

Yes but how will it write the name of the file in this format pets_feed_20080101.txt

and change the date to the currwent date

$file = "pets_feed_" . date("Ymd") . ".txt";// - for yyyymmdd
$file = "pets_feed_" . date("Ydm") . ".txt";// - for yyyyddmm
$file = "pets_feed_" . date("Ymd") . ".txt";// - for yyyymmdd
$file = "pets_feed_" . date("Ydm") . ".txt";// - for yyyyddmm

Where in the code does the date code goes?

<?php  
      
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"."\n";



$fh22 = fopen('petsfeed.txt', 'w+');

fwrite($fh22, $towrite . $fcontent);

fclose($fh);

fclose($fh22);

?>
<?php  
      
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);


$file = "pets_feed_" . date("Ymd") . ".txt";// - for yyyymmdd
  
if (!file_exists($file)) touch($file);

$fh = fopen($file, "r");

$fcontent = fread($fh, filesize($file));

$towrite = "ID Category    Description"."\n";



$fh22 = fopen($file, 'w+');

fwrite($fh22, $towrite . $fcontent);

fclose($fh);

fclose($fh22);

?>
<?php  
      
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);


$file = "pets_feed_" . date("Ymd") . ".txt";// - for yyyymmdd
  
if (!file_exists($file)) touch($file);

$fh = fopen($file, "r");

$fcontent = fread($fh, filesize($file));

$towrite = "ID Category    Description"."\n";



$fh22 = fopen($file, 'w+');

fwrite($fh22, $towrite . $fcontent);

fclose($fh);

fclose($fh22);

?>

I get this error

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /var/www/html/yumasun/media/gadzoo/txt.php on line 13

I get this error

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /var/www/html/yumasun/media/gadzoo/txt.php on line 13

Your getting that because the file was just created so file size = 0. This doesn't mean it's not working it's just letting you know that fread won't read anything because there is nothing to read yet. Let me make one adjustment.

This should clear that up.

<?php  
      
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);


$file = "pets_feed_" . date("Ymd") . ".txt";// - for yyyymmdd
  
if (!file_exists($file)) touch($file);

$fh = fopen($file, "r");
$fcontent = "";
if(filesize($file) > 0)
{
	$fcontent = fread($fh, filesize($file));
}

$towrite = "ID Category    Description"."\n";



$fh22 = fopen($file, 'w+');

fwrite($fh22, $towrite . $fcontent);

fclose($fh);

fclose($fh22);

?>

Your getting that because the file was just created so file size = 0. This doesn't mean it's not working it's just letting you know that fread won't read anything because there is nothing to read yet. Let me make one adjustment.

This should clear that up.

<?php  
      
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);


$file = "pets_feed_" . date("Ymd") . ".txt";// - for yyyymmdd
  
if (!file_exists($file)) touch($file);

$fh = fopen($file, "r");
$fcontent = "";
if(filesize($file) > 0)
{
	$fcontent = fread($fh, filesize($file));
}

$towrite = "ID Category    Description"."\n";



$fh22 = fopen($file, 'w+');

fwrite($fh22, $towrite . $fcontent);

fclose($fh);

fclose($fh22);

?>

I dont get the error any more and is writting the file to the server but is only puttting this content

ID Category Description

is suppose to look 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

I dont get the error any more and is writting the file to the server but is only puttting this content

ID Category Description

is suppose to look 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

OK, use this then.

<?php  
      
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);


$file = "pets_feed_" . date("Ymd") . ".txt";// - for yyyymmdd
  
if (!file_exists($file)) touch($file);

$fh = fopen($file, "r");

$fcontent = @fread($fh, filesize($file));

$towrite = "ID Category    Description"."\n";



$fh22 = fopen($file, 'w+');

fwrite($fh22, $towrite . $fcontent);

fclose($fh);

fclose($fh22);

?>

OK, use this then.

<?php  
      
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);


$file = "pets_feed_" . date("Ymd") . ".txt";// - for yyyymmdd
  
if (!file_exists($file)) touch($file);

$fh = fopen($file, "r");

$fcontent = @fread($fh, filesize($file));

$towrite = "ID Category    Description"."\n";



$fh22 = fopen($file, 'w+');

fwrite($fh22, $towrite . $fcontent);

fclose($fh);

fclose($fh22);

?>

Is doing the same
just writting ID Category Description to the file

Not for me, I copied that script, refreshed my page about 7 or 8 times and got this in pets_feed_20080829.txt

ID Category Description
ID Category Description
ID Category Description
ID Category Description
ID Category Description
ID Category Description
ID Category Description
ID Category Description
ID Category Description

which means that it is working fine.

Not for me, I copied that script, refreshed my page about 7 or 8 times and got this in pets_feed_20080829.txt

ID Category Description
ID Category Description
ID Category Description
ID Category Description
ID Category Description
ID Category Description
ID Category Description
ID Category Description
ID Category Description

which means that it is working fine.

This is because the file is not re writting to the file is just writting but it needs to write

ID Category Description

and the petsfeed.txt contetn

Thank you

How could I write the content on petsfeed.txt into the new file I am creating wiht the ID Category Description headers?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.