fopen() to open a File

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
greeneel
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:19 pm

fopen() to open a File

Post by greeneel »

Hi,

I have created a form to gather information from customer orders such as information and addresses and i`m trying to setup fopen() to open a text file for this I would like the folder that contains this text file to be C:\Program Files\Apache Group\Apache\Docs .. I got the following code from a website and would like to know if i`m headed in the right direction:
I am using PHP version 4.0.4pl with apache 2.0 and apache 1.3 ...
Also i`m abit new but would like to know if its possible to create a new page and access this file from the new page or can I add this to my processorder.php page that i have already created...

thx




This is the code i`ve found :

Code: Select all

<?php
   $fr = fopen('/tmp/test.txt', 'a+');
   if(!$fr) {
      echo "Error! Couldn't open the file.";
   } else {
      // $fr now can be used to represent the opened file
   }
   if(!fclose($fr)) {
      echo "Error! Couldn't close the file.";
   } 
?>
I edited to get this:

Code: Select all

<?php
   $fr = fopen(C:\Program Files\Apache Group\Apache\Docs\orders.txt', 'a+');
   if(!$fr) {
      echo "Error! Couldn't open the file.";
   } else {
      // $fr now can be used to represent the opened file
   }
   if(!fclose($fr)) {
      echo "Error! Couldn't close the file.";
   } 
?>
mod_edit: added

Code: Select all

tags[/size]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

take a look at the colors the syntax highlighter uses if you put your code in

Code: Select all

tags here.
Looks a bit queer, doesn't it?
If you run this script through the syntax checker of php (calling php -l <scriptfile name>) you get[quote]Errors parsing <scriptfile name>
PHP Parse error:  parse error, unexpected ':' in <scriptfile name> on line 2
[/quote]

Code: Select all

<?php
   $fr = fopen('C:\Program Files\Apache Group\Apache\Docs\orders.txt', 'a+');
   if(!$fr) {
      echo "Error! Couldn't open the file.";
   } else {
      // $fr now can be used to represent the opened file
   }
   if(!fclose($fr)) {
      echo "Error! Couldn't close the file.";
   }
?>
this looks more consistent. Replace the \ by / and you're done.
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

$fr = fopen('C:\Program Files\Apache Group\Apache\Docs\orders.txt', 'a+');
-OR-
$fr = fopen("C:/Program Files/Apache Group/Apache/Docs/orders.txt", 'a+');

Matching similar (single/double) quotes are important !

also, above need Write permission for the directory "C:/Program Files/Apache Group/Apache/Docs/".

You add above your code into processorder.php, as well it is also possible that you can create a new file and do "require_once('processorder.php')" in the new file. You might have to adjust the processorder.php, if you want to require_once() it.

Hope this helps.
Post Reply