Current Location: Home> Latest Articles> What Can stream_is_local Do with fopen? A Quick Method to Detect Local Files

What Can stream_is_local Do with fopen? A Quick Method to Detect Local Files

gitbox 2025-09-24

1. What is stream_is_local?

stream_is_local is a built-in PHP function used to check whether a file or stream belongs to the local file system. The function returns a boolean value: true if the given stream is a local file system resource, and false otherwise.

<span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-title function_ invoke__">stream_is_local</span></span><span> ( resource </span><span><span class="hljs-variable">$stream</span></span><span> )  
</span></span>

Parameter description:

  • $stream: A valid stream resource (which can be obtained using the fopen function).

2. How to use fopen and stream_is_local?

fopen is a PHP function used to open files. It can open local files, remote files (such as via HTTP or FTP), and more. Combined with stream_is_local, you can determine whether a file is stored on the local file system, which is especially useful when dealing with local file operations.

Basic example:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>  
</span><span><span class="hljs-variable">$file_path</span></span><span> = </span><span><span class="hljs-string">&#039;example.txt&#039;</span></span><span>;  
<p></span>// Use fopen to open the file<br>
$handle = fopen($file_path, 'r');</p>
<p>if ($handle) {<br>
// Check if the file is local<br>
if (stream_is_local($handle)) {<br>
echo "The file is local.";<br>
} else {<br>
echo "The file is not local.";<br>
}</p>

} else {
echo "Unable to open file!";
}
?>

In this example, we open the example.txt file using fopen. If the file opens successfully, we then use stream_is_local to check if the file is local.

3. Practical use cases of stream_is_local

In real-world development, stream_is_local can be applied in various scenarios. When file paths can be local, URLs, or other protocols, it helps determine whether the file is stored locally.

3.1 Handling uploaded files

When users upload files, the file path might be a URL or a local path. To confirm whether the uploaded file is local, you can use stream_is_local for verification:

... (code preserved as-is) ...

3.2 Distinguishing between remote and local files

In some cases, you may need to decide actions based on the file source (local vs remote). For instance, you might prioritize handling local files first, and download them only if they are remote.

... (code preserved as-is) ...

4. Notes

  • stream_is_local only works with stream resources. If you pass an invalid stream, the function will return false.
  • This function only determines whether the stream comes from the local file system. It does not check file content, permissions, or existence.

5. Summary

stream_is_local combined with fopen provides an effective way for developers to quickly determine whether a file originates from the local file system. This is highly practical in scenarios where distinguishing between local and remote files is necessary. By using these two functions, developers can easily make informed decisions and improve efficiency.