Skip to content

Instantly share code, notes, and snippets.

@razvanaldea89
Created November 9, 2022 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save razvanaldea89/4498c23654878a98abdb27e5f4777a66 to your computer and use it in GitHub Desktop.
Save razvanaldea89/4498c23654878a98abdb27e5f4777a66 to your computer and use it in GitHub Desktop.
Redirect to symlink fix
if ( false !== strpos( $file_path, $basedir ) ) { // File is in the uploads' folder, so we need to create the correct URL.
// Set the URL for the uploads' folder.
$file_path = str_replace( str_replace( DIRECTORY_SEPARATOR, '/', trailingslashit( $basedir ) ), str_replace( DIRECTORY_SEPARATOR, '/', trailingslashit( $uploads_dir['baseurl'] ) ), $file_path );
} elseif ( is_link( $basedir ) && false !== strpos( $file_path, readlink( $basedir ) ) ) { // File is in the uploads' folder but in symlinked directory, so we need to create the correct URL.
// Set the URL for the uploads' folder.
$file_path = str_replace( str_replace( DIRECTORY_SEPARATOR, '/', trailingslashit( readlink( $basedir ) ) ), str_replace( DIRECTORY_SEPARATOR, '/', trailingslashit( $uploads_dir['baseurl'] ) ), $file_path );
} else { // This is the case if the file is not located in the uploads' folder.
// Ensure we have a valid URL, not a file path.
$scheme = wp_parse_url( get_option( 'home' ), PHP_URL_SCHEME );
// At this point the $correct_path should have a value of the file path as the verification was made prior to this check
// If there are symbolik links the return of the function will be an URL, so the last replace will not be taken into consideration.
$file_path = download_monitor()->service( 'file_manager' )->check_symbolic_links( $file_path, true );
$file_path = str_replace( trailingslashit( $correct_path ), site_url( '/', $scheme ), $file_path );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment