-
- 18 Posts
I can't seem to find a lot about this topic. I was also looking to see if this was in the change list for recent versions.
We have the need to implement a warning to the user if the file they're uploading is going to overwrite an existing file on the server. This is a pretty simple thing to implement given the availability of a system event prior to the upload. So, I don't understand why there's only a system event fired after the file upload has already taken place. I mean where's the OnBeforeManagerFileUpload event?.
Is there something I'm missing? Is there another way to trap before the overwrite?
[ed. note: hhiers last edited this post 11 years, 4 months ago.]
-
- 534 Posts
basically the code I posted is the stripped down code from a plugin I use to sanitize filenames (because editors love to upload files that look like "f***upf!l€n@m3##justtõm4k€us& hàppy.jpg") and it does work, if a file (which name is cleaned up) already exists it just adds a _duplicate behind the name...so try it out =)...
and to your second point, you see from the above use case, that there are scenarios on which this event is very helpful, and maybe there's even a good reason that there is no OnBefore event. I'm 100% familiar how file uploading is handled, but I think the server cannot read the filename until the file is "up"...but not sure about that...
A file upload posted from a form is stored automatically by the system in a temporary location with a temporary name. In PHP, all the information for that file is provided in the $_FILES array. The file must be moved (move_uploaded_file()) or the temporary stored copy is deleted by the system (nothing to do with MODX or even PHP). So wherever your code is that moves the file is where checking for existing files of the same name would have to occur. In Revo's core, file upload management is done in core/model/modx/sources/modfilemediasource.class.php. The OnFileManagerUpload event is invoked immediately
after the move_uploaded_file() function call, around line 667 - 678
http://php.net/manual/en/function.move-uploaded-file.php
Just describing the process. There is no event for pre-handling the upload. There could be. There should be.
http://tracker.modx.com/issues/9271