1

I am getting problem while using fwrite in php. the following code works in my local computer but gives error in server.

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
   echo 'Error: '.$errno.', '.$errstr;
} else {
   fwrite($fp, 'kool');
}

There is no error with fsockopen. it passes and gives no error. fwrite is not being able to write. it fails and returns no error only false

5
  • What's the error that it's giving you? Commented Nov 16, 2010 at 16:03
  • no errors in fsockopen. its giving error in fwrite. and its blank Commented Nov 16, 2010 at 16:18
  • Are you sure fwrite is returning FALSE?... Writing 'kool' to a webserver should not work anyway. You might also want to check the return value of stream_get_meta_data. Commented Nov 16, 2010 at 16:36
  • What does it return on your local machine? Commented Nov 16, 2010 at 16:38
  • in local machine its working fine. I am getting the correct result. Commented Nov 17, 2010 at 4:22

2 Answers 2

3

This is a permissions issue with the Apache/Nobody user accessing a remote file that it doesn't have permission to modify/read/write/execute.

You should also print the error message(s) for debugging

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
    echo "Error No: ".$errno."<br />\n";
    echo "Error Str: ".$errstr."<br />\n";
} else {
    fwrite($fp, 'kool');
}
Sign up to request clarification or add additional context in comments.

1 Comment

no errors in fsockopen. its giving error in fwrite. and its blank
2

If you're on a shared host, most likely your server does not allow outbound connections on port 80. Usually only inbound connections are allowed.

4 Comments

may be you are right. I am using shared host. So what can be the alternative for me?
If you're with a larger company (BlueHost, Aplus, GoDaddy, etc) you should be able to ask their tech support to have outbound port 80 opened for you. It's a common request, they just have it turned off by default for security reasons.
is there any alternatives of this method?
That really depends on the problem you're trying to solve. If the only solution involves creating an outbound connection on a blocked port, and your host will not open that port for outbound connections, then there's not much you can do except change hosts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.