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
mattcooper
Forum Contributor
Posts: 210 Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK
Post
by mattcooper » Sun Apr 09, 2006 9:25 am
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!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Apr 09, 2006 9:30 am
your usage of fputs() is at fault. You have not opened $dest for writing.
mattcooper
Forum Contributor
Posts: 210 Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK
Post
by mattcooper » Sun Apr 09, 2006 10:00 am
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...
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Apr 09, 2006 10:05 am
you have the parameters to
fputs() in the wrong order.
mattcooper
Forum Contributor
Posts: 210 Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK
Post
by mattcooper » Sun Apr 09, 2006 10:26 am
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...
mattcooper
Forum Contributor
Posts: 210 Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK
Post
by mattcooper » Sun Apr 09, 2006 12:56 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Apr 09, 2006 1:37 pm
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'].