prinzdezibel
prinzdezibel

Reputation: 11147

Programmatically determine maximum filename length

How can I determine the maximum filename length on a linux box?

Preferred in PHP programming language.

Upvotes: 6

Views: 9249

Answers (5)

André Sousa
André Sousa

Reputation: 556

You can use the constant PHP_MAXPATHLEN

Upvotes: 2

pilcrow
pilcrow

Reputation: 58711

You want pathconf or fpathconf, which are not exposed (yet) in PHP. (When they are, they'll probably be posix_pathconf.)

You may also shell out to getconf, a command-line utility interface to the same functionality. Try this on your system:

$ getconf NAME_MAX /tmp

$ getconf PATH_MAX /tmp

Upvotes: 6

Thinker
Thinker

Reputation: 14464

I think you could use realpath(). I'm not sure best approach, but for example:

$maxlen=264-strlen(realpath('index.php')));

264 is 255(max path lim) + 9 ('index.php' len). So substracting current path length from limit gives you max current path length.

Upvotes: 0

ithcy
ithcy

Reputation: 5589

there's no need to programatically determine it. it's 255 bytes.

edit: you can have longer filenames on a very few file systems (reiser, i believe), but if you stick to 255 your app will be usable on any linux installation.

Upvotes: 5

Randolpho
Randolpho

Reputation: 56449

The maximum file length for most linux file systems is 255. You're probably best off using that as a generic constant and modifying to fit your known file system in linux. Here's a nice comparison of the file systems that might be used. Max file length is listed there.

Upvotes: 4

Related Questions