-1

I have standard html form for upload files

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
...

What is maximum length of filename, which can be uploaded by user ?

P.S.

I'm saving file on server with modified name, but also need to keep original name, because I use original name at downloading that file.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236
  • a the question is for the length of the filename or the size of the file ? If it's for the length of filename in post you have a very large limit (approching MB) – Inazo Aug 22 '18 at 15:43
  • I would check all the filename limits on all the possible operating system – RiggsFolly Aug 22 '18 at 15:44
  • @Inazo - question is for the length of the filename – Oto Shavadze Aug 22 '18 at 15:45
  • 1
    What operating system you are using? –  Aug 22 '18 at 15:45
  • 1
    I can tell you that on Windows its about 32,000 characters for filename and path but only 255 chars for just the filename – RiggsFolly Aug 22 '18 at 15:45
  • possible duplication of this thread https://stackoverflow.com/questions/15844825/filename-maxlength – afikri Aug 22 '18 at 15:46
  • @afikri If you feel that it is a duplicate, you should "flag" it as one, rather than posting it as a comment. However, that isn't a very good one, IMHO. – Funk Forty Niner Aug 22 '18 at 15:46
  • @RiggsFolly - What if user is bad guy and tries to send hacked name (i.e. not real file name, but some very long fake name) – Oto Shavadze Aug 22 '18 at 15:47
  • your question is getting more unclear, given that ^ also. – Funk Forty Niner Aug 22 '18 at 15:49
  • so, @Shujaat asked for which OS this is related to; windows? linux/unix, other? it's important to know which animal we're dealing with here. – Funk Forty Niner Aug 22 '18 at 15:49
  • Question's been closed with 2 duplicates; your answers are in there. – Funk Forty Niner Aug 22 '18 at 15:53
  • @FunkFortyNiner - user uploads `a.jpg` but tries to send veeeery long value for html form `file` parameter. Is this scenario impossible ? – Oto Shavadze Aug 22 '18 at 15:54
  • yes it is possible. – Funk Forty Niner Aug 22 '18 at 15:57
  • I don't save files with the name supplied by the user, I lie to them and make them think I saved it with that name :), `$filename = "{$user_id}".hash_file('sha1', $tempFile);` and then I save their name in the DB and my name, show them their name. Solves all kinds of issues. – ArtisticPhoenix Aug 22 '18 at 15:58
  • So, @RIKI you asked an incorrect question. Your question should be like "How to restrict length of file name?" –  Aug 22 '18 at 15:59
  • @ArtisticPhoenix - Yes, exactly, I'm just interested should I care about name length at upload? cos, I use original name at download, there is possible that user uploads file with 500 symbol but it will truncated automatically for example to 255 symbol, because my server have different config? – Oto Shavadze Aug 22 '18 at 16:10
  • I wouldn't use their name on the filesystem at all, because you can have a lot off issues with things like spaces, symbols etc. Then it's a matter of what you set the DB to, save it with your name, show them their name. – ArtisticPhoenix Aug 22 '18 at 16:14
  • @Shujaat It does not matter what OS the OP is using. A sender could be on any OS – RiggsFolly Aug 22 '18 at 16:19
  • @RiggsFolly I am agree with you. But the length of filename and path belongs to the operating system. –  Aug 22 '18 at 16:22
  • 1
    So OP needs to know what is the MAX filename lenght on ALL Operating system so he knows the largest – RiggsFolly Aug 22 '18 at 16:23
  • @RIKI - once I get my site done, I'll write a blog post on it... Don't hold your breath though I am lazy when it comes to my site. http://www.artisticphoenix.com/ – ArtisticPhoenix Aug 23 '18 at 03:19

1 Answers1

1

This problem does not belong to HTML or PHP. Its totally depend on your operating system.

In windows length of both path and filename should not exceed 260 characters. Otherwise it will give your error.

In the Windows API, the maximum length for a path is MAX_PATH, which is defined as 260 characters". The total path is, for all practical purposes, limited to 259 characters (allowing for the null-terminator)

In the case of windows, you can find out more details on the link given below... https://learn.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file