PHP: url, path, files

// url and path

function get_url_of_current_dir() {
	
	// $url = get_url_of_current_dir();
	
	/* META TAGS:
	
	ALGO: medium
	DATE: 2025-07-11 20:00
	DESC: get URL of current dir
	LANG: PHP
	PROC: no
	TAGS: url, directory, current
	
	*/
	
    if( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) $url = "https://"; 
    else $url = "http://";

    if( dirname( $_SERVER['PHP_SELF'] ) == "/" || dirname( $_SERVER['PHP_SELF'] ) == "\\" ) return $url . $_SERVER['HTTP_HOST'];
	else return $url . $_SERVER['HTTP_HOST'] . dirname( $_SERVER['PHP_SELF'] );

} // function

function get_path_of_current_dir() {
	
	// $path = get_path_of_current_dir();
	
	/* META TAGS:
	
	ALGO: medium
	DATE: 2025-07-11 20:05
	DESC: get path of current dir
	LANG: PHP
	PROC: no
	TAGS: path, directory, current
	
	*/
	
	return dirname( __FILE__ );
	
} // function

// file system

function get_files_of_dir( $dir, $recursion ) {
	
	// $files = get_files_of_dir( $dir, $recursion );
	
	/* META TAGS:
	
	ALGO: medium
	DATE: 2025-07-11 20:10
	DESC: get files of directory (with or without recursion)
	LANG: PHP
	PROC: no
	TAGS: file, directory
	
	*/
	
	$files = (
	
		new class {
			
			function get_files( $path, $ext='*', $recursion ) {
				
				$dh = opendir( $path );
				
				$files = [];
				
				while ( $file = readdir( $dh ) ) {
				
					if ( $file == '.' || $file == '..' ) continue;
					
					$f_path = str_replace( '//', '/', $path . '/' . $file );
					
					if ( is_dir( $f_path ) ) {
						
						if ( $recursion === true ) {
							
							$sub_files = $this->get_files( $f_path, $ext, $recursion );
						
							$files = array_merge( $files, $sub_files );
							
						} else {
							
							$files[] = $f_path;
							
						} // if/else
						
					} else if ( $ext == '*' || ( $f_ext = pathinfo( $f_path, PATHINFO_EXTENSION ) ) == $ext ) {
						
						$files[] = $f_path;
						
					} // if/else
					
				} // while 
				
				return $files;
				
			} // function
			
		} // class
		
	)->get_files( $dir, "*", $recursion );
	
	sort( $files );
	
	return $files;
	
} // function

function get_files_of_current_dir( $recursion ) {
	
	// $files = get_files_of_current_dir( $recursion );
	
	/* META TAGS:
	
	ALGO: medium
	DATE: 2025-07-11 20:15
	DESC: get files of current dir (with or without recursion)
	LANG: PHP
	PROC: no
	TAGS: file, directory, current
	
	*/
	
	$dir = get_path_of_current_dir();
	
	$files = get_files_of_dir( $dir, $recursion );
	
	return $files;
	
} // function