How to check if a PHP stream resource is readable or writable?

前端 未结 2 1514
后悔当初
后悔当初 2020-12-16 12:41

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

2条回答
  •  粉色の甜心
    2020-12-16 13:07

    Quite simple. Just call stream_get_meta_data($resource) from your script, then check the mode array element of the return value:

    $f = fopen($file, 'r');
    $meta = stream_get_meta_data($f);
    var_dump($meta['mode']); // r
    

    And if you want to know if the underlying data is writable:

    var_dump(is_writable($meta['uri'])); // true if the file/uri is writable
    

提交回复
热议问题