User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 455,965 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,617 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1644 | Replies: 10
Reply
Join Date: Jul 2007
Posts: 111
Reputation: Designer_101 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Designer_101 Designer_101 is offline Offline
Junior Poster

Question php reading a text file ..... help

  #1  
Nov 18th, 2007
hi, im having a bit of trouble learning php. My script is working but it is showing the amount of characters i am echoing out....heres the code.

$tireorder = 'you have ordered: '.$tireqty.' tires.' ;

//open the file and check to see if it exists//
$fp = fopen("file.txt","r+");
if(!$fp) {
echo "Your order could not be prossesed at this time";
exit;
}
//writing to a file//
fwrite($fp, $tireorder);

//closing the file//
fclose($fp);

//reading the file//
$data = @readfile("file.txt");


//echoing out//
echo $data;

?>


It writes to the text file - you have ordered: {tire amount} tires. {character amount}
Is there anyway to get rid of the character count?

all suggestions will help thanx
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: php reading a text file ..... help

  #2  
Nov 18th, 2007
I dunno, maybe you just need to write to a file:

  1. <?php
  2. $fp = fopen("file.txt","w") or die ("couldn't open!");
  3. fwrite($fp, "blah\n");
  4. fclose($fp);
  5. ?>
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Jul 2007
Posts: 111
Reputation: Designer_101 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Designer_101 Designer_101 is offline Offline
Junior Poster

Re: php reading a text file ..... help

  #3  
Nov 18th, 2007
Thanks but it didnt work .... in your example it would write out - blah 4 ...... it keeps counting my characters! Any other suggestions?
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: php reading a text file ..... help

  #4  
Nov 18th, 2007
Can you test my code on its own as a separate php file please?

When you have ran that script open the text file and tell me what you see?
Last edited by iamthwee : Nov 18th, 2007 at 1:09 pm.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Jul 2007
Posts: 111
Reputation: Designer_101 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Designer_101 Designer_101 is offline Offline
Junior Poster

Re: php reading a text file ..... help

  #5  
Nov 18th, 2007
ok, your code wrote it to the txt file perfectly but so did mine i added

<?php
$fp = fopen("file.txt","w") or die ("couldn't open!");
fwrite($fp, "blah\n");
fclose($fp);
echo $fp ;
?>

and all that echoed out was resourse id #1 ???
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: php reading a text file ..... help

  #6  
Nov 18th, 2007
In that case something else is counting the number of letters in your code when you are reading the thing back in.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: php reading a text file ..... help

  #7  
Nov 18th, 2007
Can you test the following as a separate php script and tell me what you see?

  1. <?php
  2. $filename = "file.txt";
  3. $fp = fopen($filename, "r") or die ("nope");
  4. while (!feof($fp))
  5. {
  6. $line = fgets($fp, 1024);
  7. echo "$line<br>";
  8. }
  9. ?>

If I am correct it should echo out blah
Last edited by iamthwee : Nov 18th, 2007 at 1:42 pm.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Jul 2007
Posts: 111
Reputation: Designer_101 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Designer_101 Designer_101 is offline Offline
Junior Poster

Re: php reading a text file ..... help

  #8  
Nov 18th, 2007
ok il have a look thanks
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: php reading a text file ..... help

  #9  
Nov 18th, 2007
what happend?
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Jul 2007
Posts: 111
Reputation: Designer_101 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Designer_101 Designer_101 is offline Offline
Junior Poster

Re: php reading a text file ..... help

  #10  
Nov 18th, 2007
YES! it works but still i do not understand the code ... could you explain ?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:03 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC