One possible way would be to calculate the MD5 signature of the file (or the file ID in a database), and then build/find the path based on that.
For example, say we get an MD5 signature like "1ff8a7b5dc7a7d1f0ed65aaa29c04b1e"
The path might look like "/1f/f" or "/1f/ff/8a"
The reason is that you don't want to have all the files in 1 folder, and you want to have the ability to "partition" them across different servers, or a SAN or whatever in an equally-spread-out way.
The MD5 signature is a string of 16 "hex" characters. So our example of "/1f/ff/8a" gives us 256*256*256 folders to store the files in. That ought to be enough for anybody :)
Update, due to popular demand:
NOTE - I just realized we are talking specifically about how MediaWiki does it. This is not now MediaWiki does it, but another way in which it could have been done.
By "MD5 signature" I mean doing something like this (code examples in Perl):
use Digest::MD5 'md5_hex';
my $sig = md5_hex( $file->id );
$sig is now 32 alpha-numeric characters long: "1ff8a7b5dc7a7d1f0ed65aaa29c04b1e"
Then build a folder structure like this:
my $path = '/usr/local/media';
map { mkdir($path, 0666); $path .= "/$_" } $sig =~ m/^(..)(..)(..)/;
open my $ofh, '>', "$path/$sig"
or die "Cannot open '$path/$sig' for writing: $!";
print $ofh "File contents";
close($ofh);
Folder structure looks like
/
usr/
local/
media/
1f/
f8/
a7/
1ff8a7b5dc7a7d1f0ed65aaa29c04b1e