Jump to content

Random Images file name


Recommended Posts


I'm trying to change all upload image files names for the random names.

So I put in Pageimage.php this lines:

$rand = substr(md5(microtime()),rand(0,26),5);
$basename = basename($rand, "." . $this->ext()); 		// i.e. myfile

But as a results I got a lot of random filenames during each page loading.

Please advise what do I miss?



Link to comment
Share on other sites

Thanks a lot, Kongondo.

That seems to be right. I'll think twice how to manage it in a deferent way.

For example, if I upload "Martin_Johnson_House,_SIO,_La_Jolla_Shores_-_at_sunset_pano.jpg" I get "Martin_Johnson_House,_SIO,_La_Jolla_Shores_-_at_sunset_pano.640x480.jpg" and I would like to get "rndmname.640x480.jpg"

Link to comment
Share on other sites

Do you want to rename the original files on upload?
If yes, here are some pieces of code I have collected in the forum from Soma. This could be a starting point for a module:

class ImageRenameOnUpload extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'ImageRenameOnUpload',
            'version' => 100,
            'summary' => '',
            'href' => '',
            'singular' => true,
            'autoload' => true

    public function init() {
        $this->addHookBefore('InputfieldFile::fileAdded', $this, 'renameImage');

    public function renameImage($event) {
        $inputfield = $event->object;
        if($inputfield->name != 'images') return;      // we assume images field !! name of the field is: images !! otherwise change it

        $p = $inputfield->value['page'];               // get the page
        $image = $event->argumentsByName("pagefile");  // get the image
        $ext = $image->ext;                            // get extension
        $newBasename = 'my_random_name_' . time();     // create your random name here !!

        $p->images->trackChange("basename");           // prepare page to keep track for changes on basename
        //$image->removeVariations();                  // isn't needed here, I think
        $image->rename($newBasename . '.' . $ext);     // rename the image
        $p->save('images');                            // save the page

As of this code will work, you have to add something that refreshs your admin page at the end, because upload is via ajax request and the label presenting your imagefilename isn't updated now.

But should be a good starting point.

EDIT: Have changed  addHookAfter  to  addHookBefore and then it all works, including updating the label with filename!

Don't know really why in detail. Just have found out by trying! :)

Edited by horst
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...