We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12117
    • 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.]
      • 40045
      • 534 Posts
      I'm sure an event like OnBeforeFileManagerUpload would be a nice addition, why not file a ticket in the bug tracker for that?

      meanwhile you could try something like that in a plugin OnFileManagerUpload (untested):

      <?php
      foreach( $files as $file ) {
      	if ( $file['error'] == 0 ) {
      		$pathInfo = pathinfo($file['name']);
      		$basePath = $source->getBasePath();
      
      		$path = $directory . $file['name'];
      		
      		if ( file_exists($basePath . $directory . $path) ) {
      			$newPath = $pathInfo['filename'] . '_duplicate.' . $pathInfo['extension'];
      		}
      
      		$source->renameObject($path, $newPath);
      	}
      }
      


      basically this would add a "_duplicate" to a file that already exists, so it won't override the original one...not sure if this helps in your scenario...but throwing a warning would probably include some extJS work and there I'm not able to help^^ [ed. note: exside last edited this post 11 years, 4 months ago.]
        • 12117
        • 18 Posts
        I haven't tested this either, but I'd be willing to bet your solution doesn't work. The OnFileManagerUpload event is invoked after the files have been written to the server with the move_upoaded_files() function. I don't think you can resurrect the old files at that point...but I've been wrong before. smiley

        I might have filed a bug, but I thought I'd check to see if anyone had a workaround, because I don't have time to wait for it to be added to the queue. I'm still kind of amazed that the OnFileManagerUpload event even exists (in the sequence at least). I mean, what would be more useful, an event to handle files before or after they're written to disk? I don't need an event to do something with the files after the fact. I can write a script to do that functionality.
          • 40045
          • 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

              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 12117
              • 18 Posts
              Quote from: sottwell at Dec 12, 2012, 05:34 PM
              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

              Thanks for clarifying the upload process, Susan. I'm a little confused though. Are you implying an avenue for a fix or are you confirming that there's no event to pre-handle the upload?
              • 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
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 12117
                  • 18 Posts
                  Quote from: sottwell at Dec 12, 2012, 06:22 PM
                  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

                  Hey, you added one too! smiley I added one earlier specific to our version (2.2.0) on exside's suggestion. Thank's for the confirm on this.
                    • 12117
                    • 18 Posts
                    Quote from: sottwell at Dec 12, 2012, 06:22 PM
                    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

                    Right...there should be one! We're at the point where we're trying to do all of the customizations for our user experience (via plugins) and this is a pretty obvious one - so users don't obliterate their resources. It's frustrating because it's a 2 second plugin...if you have the event. I wanted to pull my hair out when I traced the OnManagerFileUpload event only to find out it was a post-action event!