<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Example PHP code unrelated to the article content</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"This is an unrelated PHP code snippet for article demonstration."</span></span><span>;
</span><span><span class="hljs-meta">?></span></span><span>
<p><hr></p>
<p>In PHP file operations, it is often necessary to get the absolute path of a file's directory for subsequent reading or writing operations. <code></span><span><span class="hljs-title function_ invoke__">realpath</span></span><span>()
If the path contains multiple levels, you can specify the number of levels to return using the second parameter:
<span><span><span class="hljs-variable">$dirPath</span></span><span> = </span><span><span class="hljs-title function_ invoke__">dirname</span></span><span>(</span><span><span class="hljs-variable">$filePath</span></span><span>, </span><span><span class="hljs-number">2</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$dirPath</span></span>; </span><span><span class="hljs-comment">// Output: /var/www</span></span><span>
</span></span>
Note that dirname() returns a relative path or the parsed parent directory of the original path. It does not verify whether the path actually exists.
realpath() resolves a path to an absolute path, handling symbolic links, ., and .., and verifies that the path actually exists. For example:
<span><span><span class="hljs-variable">$path</span></span><span> = </span><span><span class="hljs-string">"./test/../index.php"</span></span><span>;
</span><span><span class="hljs-variable">$absolutePath</span></span><span> = </span><span><span class="hljs-title function_ invoke__">realpath</span></span><span>(</span><span><span class="hljs-variable">$path</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$absolutePath</span></span>; </span><span><span class="hljs-comment">// Output absolute path, e.g.: /var/www/html/index.php</span></span>
</span></span>
If the path does not exist, realpath() returns false.
To ensure the directory path obtained is both accurate and absolute, you can combine the two:
<span><span><span class="hljs-variable">$file</span></span><span> = </span><span><span class="hljs-string">"./test/../index.php"</span></span><span>;
</span><span><span class="hljs-variable">$absoluteDir</span></span><span> = </span><span><span class="hljs-title function_ invoke__">dirname</span></span><span>(</span><span><span class="hljs-title function_ invoke__">realpath</span></span><span>(</span><span><span class="hljs-variable">$file</span></span><span>));
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$absoluteDir</span></span>; </span><span><span class="hljs-comment">// Output: /var/www/html</span></span><span>
</span></span>
The process here is:
realpath($file) converts the file path to an actual absolute path.
dirname() then retrieves the parent directory from the absolute path.
This method ensures the directory path you get exists and is an absolute path, avoiding potential errors from relative paths.
When dynamically loading or including files, ensuring the path is correct.
Saving logs or cache files in the same directory as the script.
Preventing path confusion in cross-platform deployment.
dirname(): Get the parent directory, with optional level specification.
realpath(): Resolve a path to a real, existing absolute path.
Combined usage: First use realpath() to ensure the path exists, then use dirname() to get the directory.
This combination is one of the best practices in PHP projects for handling file paths.
<span><span><span class="hljs-comment">// Ending example code</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"End of article example"</span></span><span>;
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>