Page 1 of 1
can't use the rewind function?
Posted: Thu Aug 29, 2002 11:02 am
by jmandango
Hey Folks, Need a little help here.
I am trying to impliment a simple counter that uses a text file to write to. I am putting the code below. My problem is that when it gets to the rewind part it errors and gives the following message:
Warning: Supplied resource is not a valid File-Handle resource in /usr/local/home/domain.com/httpdocs/count.php on line 6
Here is my code....any ideas or fixes would be greatly appreciated as always.
Code: Select all
<?php
$fp = fopen ("http://www.domain.com/data.txt","r+");
flock($fp,1);
$count = fgets ($fp,6);
$count += 1;
rewind ($fp);
fputs ($fp,$count);
flock ($fp,3);
fclose ($fp);
echo "$count";
?>
[/i]
Posted: Thu Aug 29, 2002 11:29 am
by volka
probably you can't rewind a url-wrapped filestream.
What you got is done and will not come again

unwrapping a url-wrapped filestream?
Posted: Thu Aug 29, 2002 12:44 pm
by jmandango
So then how would I fix this? Can it be unwrapped and then rewound? I am at a loss right now.
thanks,
Jmandango
Need an easy php counter
Posted: Fri Aug 30, 2002 8:31 am
by jmandango
I have tried unsuccessfully to impliment a simple php counter on my site to no avail. This is the code I used:
Code: Select all
<?php
$fp = fopen ("http://www.domain.com/countdata.txt","r+");
flock($fp,1);
$count = fgets ($fp,6);
$count += 1;
rewind ($fp);
fputs ($fp,$count);
flock ($fp,3);
fclose ($fp);
echo "$count";
?>
However, this just gets me errors. I also tried the tutorials on the devnetwork site but cannot get anything with those either. Can somebody please, please help me or point me to something that works? Thanks so very much!!!!
Posted: Fri Aug 30, 2002 8:32 am
by hob_goblin
change the url:
http://www.foo.bar/foobar
to the absolute path
usr/foo/bar
try that, and no - you can't open files on other servers using the functions you used, most php functions for filesystem only apply to files on the users own filesystem
Posted: Fri Aug 30, 2002 4:00 pm
by Takuma
You can't open other sites things cos of security issues and other things... Like if you can read PHP file that stores username and password that's is BAD...
remote files using fopen()
Posted: Sun Sep 01, 2002 4:33 am
by gite_ashish
Hi,

See the PHP man:
http://www.php.net/manual/en/function.fopen.php
* fopen() can be used to open file or URL.
* HTTP connections are read-only; you cannot write data or copy files to an HTTP resource.
* You can open files for either reading or writing via ftp (but not both simultaneously). If the remote file already exists on the ftp server and you attempt to open it for writing, this will fail.
* If you need to update existing files over ftp, use ftp_connect().
I am currently working on the sample code for "easy php counter". Will post the code...
Regards,
counter... fopen... rewind... !
Posted: Sun Sep 01, 2002 7:35 am
by gite_ashish
Code: Select all
<?php
error_reporting( E_ALL );
//
// the remote file, local file and their respective dir
// should have proper (write) permission for php
//
// config
$ftpServer = '192.168.1.61';
$ftpLogin = 'test';
$ftpPasswd = 'test123';
$ftpDir = 'count';
$ftpFile = 'remote.count.txt';
$localFile = 'local.count.txt';
// open *local* file stream
$fpl = fopen( $localFile, 'r+' );
// connect to *remote* ftp server
$fpr = ftp_connect( $ftpServer );
ftp_login( $fpr, $ftpLogin, $ftpPasswd );
ftp_chdir( $fpr, $ftpDir );
// download remote file into (overwrite) local file
ftp_fget( $fpr, $fpl, $ftpFile, FTP_ASCII );
// read contents from local file
rewind( $fpl );
$count = fgets( $fpl );
// update counter
echo "old count: $count<BR>";
$count += 1;
echo "new count: $count<BR>";
// write contents to local file
rewind( $fpl );
$i = fputs( $fpl, $count );
echo "$i byte(s) written in $localFile";
// upload local file into (overwrite) remote file
rewind( $fpl );
ftp_fput( $fpr, $ftpFile, $fpl, FTP_ASCII );
// clean up
ftp_close( $fpr );
fclose( $fpl );
?>