title: How to check if a PHP stream resource is readable or writable?
body: <p>In PHP, how do I check if a stream resource (or file pointer, handle, or whatever you want to call them) is either readable or writable? For example, if you're faced with a situation where you know nothing about how the resource was opened or created, how do you check if it's readable? And how do you check if it's writable?</p>
  
  <p>Based on the testing that I've done (just with regular text files using PHP 5.3.3), <a href="http://www.php.net/manual/en/function.fread.php" rel="noreferrer"><code>fread()</code></a> does not throw any errors at any level when the resource is not readable. It just returns an empty string, but it also does that for an empty file. And ideally, it would be better to have a check that doesn't modify the resource itself. Testing if a resource is readable by trying to read from it will change the position of the pointer.</p>
  
  <p>Conversely, <a href="http://www.php.net/manual/en/function.fwrite.php" rel="noreferrer"><code>fwrite()</code></a> does not throw any errors at any level when the resource is not writable. It just returns zero. This is slightly more useful, because if you were trying to write a certain number of bytes to a file and <code>fwrite()</code> returns zero, you know something went wrong. But still, this is not an ideal method, because it would be much better to know if it's writable before I need to write to it rather than trying to write to it and see if it fails.</p>
  
  <p>Also, ideally, the check should work on any sort of stream resource, not just files.</p>
  
  <p>Is this possible? Does anything like this exist? I have been unable to find anything useful. Thanks in advance for your answers.</p>