Comma in file name (v2022)

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
bkonia
User
Posts: 141

Comma in file name (v2022)

Post by bkonia »

In PHPMaker 2022, if you upload a file containing a comma, the file will upload fine, but if you try to delete it, nothing happens. I assume this is happening because PHPMaker uses a comma delimiter to separate files in multiple file fields, and it's not properly escaping commas in uploaded file names. It happens even in single file fields, where the Multiple box is disabled.


arbei
User
Posts: 9384

Post by arbei »

You should avoid using comma in file name, if you must, you should change the Multiple file upload separator.


bkonia
User
Posts: 141

Post by bkonia »

I know the delimiter can be changed, but that doesn't address the issue. This is a public-facing web application, so we have no control over the characters customers may use in the files they upload. I guess we could strip out commas in the Row_Inserting event, but that would only work on single-file fields. With multiple-file fields, we'd have no way of knowing which commas are delimiters and which are part of file names. PHPMaker should automatically URL-encode, or remove whatever character is being used as the delimiter.


mobhar
User
Posts: 11727

Post by mobhar »

Since you have already known that the delimiter can be changed to the character other than comma, then there should not be the reason anymore you do not know which one is the delimiter (which is not comma anymore). That means, for such case, then the comma character is definitely the part of filename (if only you have already changed the delimiter).

As you mentioned, you may strip out the commas from the filename in Row_Inserting server event (if you want).


bkonia
User
Posts: 141

Post by bkonia »

The point is, a customer can upload a file containing ANY character, so changing the delimiter doesn't help at all. And sure, we can strip out the delimiter character in the Row_Inserting event, but that will only work with single-file fields. PHPMaker should handle this automatically, by stripping or escaping the delimiter character in uploaded files.


mobhar
User
Posts: 11727

Post by mobhar »

That's why Row_Inserting and/or Row_Updating server events are for. You may rename the file or even multiple files in those events to the ones that suits your needs.

This might help you: How to rename multiple files and displayed it vertically (v2022). It should work also for v2023.


bkonia
User
Posts: 141

Post by bkonia »

Sorry, but it's inconvenient and unprofessional that PHPMaker doesn't handle this automatically. It's as simple as:

$fname = str_replace(Config("MULTIPLE_UPLOAD_SEPARATOR"), "", $fname);

We shouldn't have to add custom code on every page where multiple upload fields exist.


mobhar
User
Posts: 11727

Post by mobhar »

It is impossible to handle it automatically, since not everyone needs will be the same, isn't?


bkonia
User
Posts: 141

Post by bkonia »

I don't understand your point.

The purpose of using the MULTIPLE_UPLOAD_SEPARATOR constant is to automatically adapt to whatever character is being used as the separator. When this character appears in the filename, PHPMaker would replace it with its hex version, before storing the filename in the database. Storing the separator character unescaped will always result in a broken link. Therefore, there can never be a circumstance in which it should be stored unescaped. Thus, your statement, not everyone's needs will be the same, is incorrect. No one who wants filenames stored incorrectly in the database, since that results in broken links in the UI.

Example:

  1. Assume the separator character is a comma, which is the default:
  2. Assume the user uploads a file called my,file.jpg
  3. The file will be correctly uploaded to the server as my,file.jpg. This is a legal filename.
  4. The filename will be incorrectly stored in the database as my,file.jpg. Why is this incorrect? It's incorrect because the comma is stored unescaped. Thus, when PHPMaker renders the file in the UI, it will be displayed as two separate files: my and file.jpg. Since neither of these files exist on the server, both links will be broken.

Instead, PHPMaker should replace the comma with %2C before storing the filename in the database. That way, it will appear to PHPMaker as a single file, not two broken files, but when it's rendered, the %2C will be correctly displayed as a comma.


mobhar
User
Posts: 11727

Post by mobhar »

My point is, some developers allow their end-users to upload file which has comma character in its filename. For this, then they already realized to adjust/change the upload separator character to the other that will be different than comma.

Since the default upload separator has been changed from comma to other character, then the comma separator should not be handled automatically by removing that comma character just like your code above. This is what I meant also with "since not everyone needs will be the same, isn't?".


bkonia
User
Posts: 141

Post by bkonia »

My code is not specific to the comma character. It uses the MULTIPLE_UPLOAD_SEPARATOR constant, which is set to whichever character the developer has chosen as the separator. For example, if the developer decides to use the exclamation point character as the separator, the MULTIPLE_UPLOAD_SEPARATOR constant will contain the exclamation point character.

My suggestion is to replace any instances of the separator character in the filename with its hex equivalent. In the example of the exclamation point, the hex code is 21. So, the filename my!file.jpg would be stored in the database as my%21file.jpg. When PHPMaker renders the page, it will see a single file, not two files separated by an exclamation point. However, since hex coding is the standard way to escape characters URLs, the link will work correctly.


Post Reply