Reading several questions regarding the topic, I made my function to return file's absolute path from the file URL. As I'm using this in an WordPress environment, hence I availed untrailingslashit()
in it.
/**
* Get file absolute path from URL.
*
* @author carlosveucv
* @link https://stackoverflow.com/a/17744219/1743124
*
* @param string $file_url File URL.
* @return string File path.
* --------------------------------------------------------------------------
*/
function wp20171021_get_file_path_from_url( $file_url ) {
$file_path = parse_url( $file_url, PHP_URL_PATH );
return wp_normalize_path( $_SERVER['DOCUMENT_ROOT'] . $file_path );
}
- How bulletproof is this?
- What kind of caveats this might have in real world?
- I tested this in Windows, so is it 'cross platform-friendly?