can't use the rewind function?

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
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

can't use the rewind function?

Post 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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

probably you can't rewind a url-wrapped filestream.
What you got is done and will not come again ;)
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

unwrapping a url-wrapped filestream?

Post 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
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

Need an easy php counter

Post 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!!!!
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post 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...
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

remote files using fopen()

Post by gite_ashish »

Hi,

:idea: 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,
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

counter... fopen... rewind... !

Post by gite_ashish »

Code: Select all

&lt;?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&lt;BR&gt;";
    $count += 1;
    echo "new count: $count&lt;BR&gt;";

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

?&gt;
Post Reply