infamous FOPEN error

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
ziggy1621
Forum Commoner
Posts: 37
Joined: Mon Sep 12, 2005 5:12 pm

infamous FOPEN error

Post by ziggy1621 »

getting the errors:

Code: Select all

Warning: fopen(/hsphere/local/home/sadmi/site.com/test/text.txt): failed to open stream: Permission denied in /hsphere/local/home/sadmi/site.com/test/test1.php on line 7

Warning: ftp_fget() expects parameter 2 to be resource, string given in /hsphere/local/home/sadmi/site.com/test/test1.php on line 12
with code:

Code: Select all

1    <?php
2
3    
4
5    $filelocation = $_SERVER['DOCUMENT_ROOT'] . '/test/' . 'text.txt'; 
6    $source = "http://www.site.com/test/test1.txt";
7    $target = fopen("$filelocation", "w");
8
9    $conn = ftp_connect("site.com") or die("Could not connect");
10   ftp_login($conn,"login","password");
11
12  ftp_fget($conn,$source,$target,FTP_ASCII);
13
14  ftp_close($conn);
?>
added the numbers so you wouldn't have to count ;)

thanks in advance

ziggy
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: infamous FOPEN error

Post by timvw »

ziggy1621 wrote: Warning: fopen(/hsphere/local/home/sadmi/site.com/test/text.txt): failed to open stream: Permission denied in /hsphere/local/home/sadmi/site.com/test/test1.php on line 7
I believe this is expected behaviour and is even mentionned somewhere in the manual. If you open a resource over http stream than you can't write to it.
ziggy1621 wrote: Warning: ftp_fget() expects parameter 2 to be resource, string given in /hsphere/local/home/sadmi/site.com/test/test1.php on line 12
bool ftp_fget ( resource ftp_stream, resource handle, string remote_file, int mode [, int resumepos] )

You're using $source as the second parameter (a string) where it expects a resource (a filepointer like $target)...
Well, in your code $target isn't a resource either, but NULL because it can't open filestreams for writing with a http stream..

Btw, why are u using "$string" if you can use $string instead?
ziggy1621
Forum Commoner
Posts: 37
Joined: Mon Sep 12, 2005 5:12 pm

Post by ziggy1621 »

got the first error fixed... was a chmod...duh

thx for the pointer on the second. seems i had $source and $target swapped.

now i got it write and its saying that the $source (text1.txt) does not exist, but I'm looking at it right now.

weird

But let me backup a bit.... I'm trying to write code that will create a text file, or open an existing one and add to it then close... what I'm doing above is just warming up. I've only used FTP syntax in PHP before to upload images.

thanks,

ziggy
Post Reply