Page 1 of 1
fgets() and fputs() causing problems
Posted: Sun Apr 09, 2006 9:25 am
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!
Posted: Sun Apr 09, 2006 9:30 am
by feyd
your usage of fputs() is at fault. You have not opened $dest for writing.
Posted: Sun Apr 09, 2006 10:00 am
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...
Posted: Sun Apr 09, 2006 10:05 am
by feyd
you have the parameters to
fputs() in the wrong order.
Posted: Sun Apr 09, 2006 10:26 am
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...

Posted: Sun Apr 09, 2006 12:32 pm
by John Cartwright
Posted: Sun Apr 09, 2006 12:56 pm
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
Posted: Sun Apr 09, 2006 1:37 pm
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'].