Answers.Body: <h1>Use the assets.onBeforeSaveAsset event</h1>
  <p>You could write a plugin that listens for when an asset is saved via the <code>assets.onBeforeSaveAsset</code> event and rename the image before it is saved to the database?</p>
  <p>So you could have something like this in your plugins init method:</p>
  <pre><code>craft()-&gt;on('assets.onBeforeSaveAsset', function(Event $event) {
  
      // Only rename if it’s a new asset being saved
      if($event-&gt;params[‘isNewAsset’])
      {
          $asset = $event-&gt;params[‘asset’];
  
          $asset-&gt;setAttribute(‘filename’, ‘SOME_RANDOM_STRING’);
      }
  }
  </code></pre>
  <p>I haven’t tested this code, but from looking at Craft’s source and the docs this should work.</p>
  <p><a href="http://buildwithcraft.com/docs/plugins/hooks-and-events#events" rel="nofollow noreferrer">Documentation about events can be found on the Craft website</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-14T22:01:56.650Z
Answers.Text: Hi @Alec-Ritson :) If you don't mind answering a probably dumb question, should I add something in 'SOME_RANDOM_STRING'?
Answers.UserDisplayName: rey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-14T22:07:26.150Z
Answers.Text: Yes you would replace it with whatever random name you want to generate, like in your question: 2680542675157_cc7m541v_l.jpg
Answers.UserDisplayName: Alec Ritson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-14T22:10:26.190Z
Answers.Text: But that would generate a different random name for every image? Or that would do for one image only?
Answers.UserDisplayName: rey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-14T23:13:01.017Z
Answers.Text: You would just need a way of generating a unique filename for each asset that's uploaded, so you could hash the initial filename then time stamp it, or you could look at [hashids](https://github.com/ivanakimov/hashids.php) for generating random hashes from id's (if there is a user id for example) it's just down to preference on how you want the filenames stored :)
Answers.UserDisplayName: Alec Ritson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-16T07:36:19.490Z
Answers.Text: I'm sorry, I'm a terrible developer >.< Would be possible to use something like this for Craft? http://stackoverflow.com/a/3261107
Answers.UserDisplayName: rey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-12T15:57:51.407Z
Answers.LastActivityDate: 2017-10-06T17:01:19.837Z
Answers.LastEditDate: 2020-06-17T08:56:27.903Z
Answers.UserDisplayName: Alec Ritson
Body: <p>How to automatically rename the image files to a random string during the upload process? For example, I have an image named "big_poster.jpg" and I want it to be saved as something random, for example "2680542675157_cc7m541v_l.jpg" </p>
  
  <p>I see every asset have a "Filename" and "Title" field. Would be possible to use these fields to generate a random string? Perhaps using the <a href="https://github.com/sjelfull/Craft-Randomm/" rel="nofollow">Randomm</a> plugin?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-01-04T18:13:26.090Z
Comments.Text: Where to put that code? Can I create just an empty module with that code? because tried but nothing...
Comments.UserDisplayName: Vitalie Jalbu
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-01-06T16:22:06.767Z
Comments.Text: This does not provide an answer to the question. Once you have sufficient [reputation](https://craftcms.stackexchange.com/help/whats-reputation) you will be able to [comment on any post](https://craftcms.stackexchange.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/11326)
Comments.UserDisplayName: Jalen Davenport
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-29T02:49:32.817Z
Comments.Text: Curiously, what's the use case for this?
Comments.UserDisplayName: Michael Rog
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-11-02T22:27:49.110Z
Comments.Text: I'd like this as well. I have users adding photos from the front end and it would be nice to be able to organise them automatically as I have no control over what there original files are named.
Comments.UserDisplayName: shorn
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-11-08T22:00:43.137Z
Comments.Text: @shorn Exactly!
Comments.UserDisplayName: rey
Tags: <url>
Title: How to automatically rename image uploads?
ThreadText: # How to automatically rename image uploads?
  
  (Asked by: username_0 on 2014-10-29)
  
  How to automatically rename the image files to a random string during the upload process? For example, I have an image named "big\_poster.jpg" and I want it to be saved as something random, for example "2680542675157\_cc7m541v\_l.jpg"
  
  I see every asset have a "Filename" and "Title" field. Would be possible to use these fields to generate a random string? Perhaps using the [Randomm](https://github.com/sjelfull/Craft-Randomm/) plugin?
  
  **Tags:** url
  
  > **Comments:**
  > **username_3**: Curiously, what's the use case for this?  
  > **username_4**: I'd like this as well. I have users adding photos from the front end and it would be nice to be able to organise them automatically as I have no control over what there original files are named.  
  > **username_0**: @username_4 Exactly!  
  > **username_1**: Where to put that code? Can I create just an empty module with that code? because tried but nothing...  
  > **username_2**: This does not provide an answer to the question. Once you have sufficient [reputation](https://craftcms.stackexchange.com/help/whats-reputation) you will be able to [comment on any post](https://craftcms.stackexchange.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/11326)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_5 on 2014-11-12. Score: 2
  
  # Use the assets.onBeforeSaveAsset event
  
  You could write a plugin that listens for when an asset is saved via the `assets.onBeforeSaveAsset` event and rename the image before it is saved to the database?
  
  So you could have something like this in your plugins init method:
  
  ```
  craft()->on('assets.onBeforeSaveAsset', function(Event $event) {
  
      // Only rename if it’s a new asset being saved
      if($event->params[‘isNewAsset’])
      {
          $asset = $event->params[‘asset’];
  
          $asset->setAttribute(‘filename’, ‘SOME_RANDOM_STRING’);
      }
  }
  
  ```
  
  I haven’t tested this code, but from looking at Craft’s source and the docs this should work.
  
  [Documentation about events can be found on the Craft website](http://buildwithcraft.com/docs/plugins/hooks-and-events#events)
  
  > **Comments:**
  > **username_0**: Hi @username_5-username_5 :) If you don't mind answering a probably dumb question, should I add something in 'SOME_RANDOM_STRING'?  
  > **username_5**: Yes you would replace it with whatever random name you want to generate, like in your question: 2680542675157_cc7m541v_l.jpg  
  > **username_0**: But that would generate a different random name for every image? Or that would do for one image only?  
  > **username_5**: You would just need a way of generating a unique filename for each asset that's uploaded, so you could hash the initial filename then time stamp it, or you could look at [hashids](https://github.com/ivanakimov/hashids.php) for generating random hashes from id's (if there is a user id for example) it's just down to preference on how you want the filenames stored :)  
  > **username_0**: I'm sorry, I'm a terrible developer >.< Would be possible to use something like this for Craft? http://stackoverflow.com/a/3261107  
  
  ---
  
  