...

View Full Version : Problems with fopen



cyphix
05-13-2004, 11:00 PM
It keeps reporting problems with line 5 in this code..



<?php

$file = fopen("num.txt", "r+")

fputs($file, "$file++");

fclose($file);

echo ($file);

?>


Whats the big problem?

sidney
05-13-2004, 11:30 PM
missing a semi colon on line 3

cyphix
05-13-2004, 11:34 PM
Yeah.. finally spotted that heh...

But now I am getting weird results..

When I run it I get a print out of this..



Resource id #2


The original file has the number "100" in it & this is what I get by adding +1 to it????

All I am trying to do is increnement the files number by 1 & then get that new number in a variable.

sidney
05-14-2004, 12:01 AM
fopen links $file to a resourceid which is the pointer to the files internal position you would then need to use fread() to read the content using the $file resouceid and a loop

a better way to read the contents of the file in to a string is below


<?php
$file="num.txt";
$content = file_get_contents($file);
$content++;
$resourceid = fopen($file, "w");
fputs($resourceid, $content);
fclose($resourceid);
echo ($content);

cyphix
05-14-2004, 12:08 AM
Ahh.. I see what you mean! :-)

Thanks for the code, works good! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum