fgets() and fputs() causing problems

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
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

fgets() and fputs() causing problems

Post by mattcooper »

Hi,

I'm trying to copy the contents of a sorce file into a newly created file:

Code: Select all

$filename = "index.php";
$dest = "$ezine_id/$filename";
echo $filename."<br>".$dest;
$datafile = fopen($filename, "w+");
while(!feof($datafile)) {
    $code = fgets($datafile);
 fputs($code,$dest);
}
fclose($filename);
I'm getting an "invalid resource stream" error and I can't figure out why. The "index.php" file is being created successfully.

Can anyone advise as to why this may be?

Thanks in advance!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your usage of fputs() is at fault. You have not opened $dest for writing.
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

feyd wrote:your usage of fputs() is at fault. You have not opened $dest for writing.
Using this code, I'm getting the same result. What the h*ll is wrong with it? Makes little sense to me...

Code: Select all

$filename = "index.php";
$dest = fopen("$ezine_id/$filename", "w+");
$datafile = fopen($filename, "r");
while(!feof($datafile)) {
    $code = fgets($datafile);
if(!fputs($code,$dest)){ echo "Unsuccessful";}
}
fclose($datafile);
fclose($dest);
Thanks...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have the parameters to fputs() in the wrong order.
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Yes, just spotted that! But, using this code (which I hoped would be the final version!), all is well except that the contents of the file aren't being copied over...

Code: Select all

$filename = "index.php";
$dest = fopen("$ezine_id/$filename", "w+");
if(!chmod("$ezine_id/$filename", 0755)){ echo "Couldn't CHMOD this file";}  
$datafile = fopen($filename, "r");
while(!feof($datafile)) {
    $code = fgets($datafile);
if(!fputs($dest,$code)){ echo "Unsuccessful";}
}
fclose($datafile);
fclose($dest);
The chmod is happening, but the fputs() is still failing.

Thanks again... :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

why not use copy()?
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

It was my lack of success with copy() that made me explore this method! Using this code:

Code: Select all

$filename = "/$ezine_id/index.php";
$source = "index.php";

copy($source,$filename);
...returns this error:
Warning: copy(/3913389/index.php): failed to open stream: No such file or directory in /home/sageinte/public_html/eman/create.php
I agree that copy() would be much simpler and really would prefer to use it. Any idea why I might be getting the error?

Cheers
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

copy's paths are file system relative, not web/document root relative. So /3913389 is pointing at a directory in the root of the server. You may want to use $_SERVER['DOCUMENT_ROOT'].
Post Reply