We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39115
    • 15 Posts
    Do you know effective way to do this? For example I'm uploading file 1.jpg and after it I'm trying to upload it again. The file will be overwritten. Can new files with the same file-name have file-names with some postfix like 1_2.jpg, 1_3.jpg etc?

    I'm using MODX Revolution 2.2.2-pl (traditional) with FileUpload Snippet (http://bobsguides.com/fileupload-tutorial.html)
      • 39115
      • 15 Posts
      I have found the solution. I based on post:

      https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb

      I that line I have pasted the method that checked if uploaded file exists and add to the name post fix:

      in:

      /core/model/modx/sources/modfilemediasource.class.php


      I declare method:

      public function smartDoNotReplaceDuplicates($name) {
      
      		if (file_exists($name)) {
      			$extention = explode(".",$name);
      			$extention = end($extention);
      			
      				$file=$name;
      				$extention = explode(".",$file);
      				$extention = end($extention);
      				$killDuplicate = substr($file,0,strlen($file)-strlen($extention)-1);
      				return $this->smartDoNotReplaceDuplicates($killDuplicate."_2.".$extention);
      			
      		} else {
      			return $name;
      		}
          }
      


      and in method:

      uploadObjectsToContainer


      after line

      $newPath = $directory->getPath().$newPath;


      I added this line :

      $newPath = $this->smartDoNotReplaceDuplicates($newPath);