Jump to content

Non-repetitive random string generator?


Michdd

Recommended Posts

Is there some kind of what that I can make this? Like, say image hosting sites, how they generate a different URL to save the image as everytime because sometimes someone might try to upload a image name that someone else already entered.

 

Basically I want something that will generate a random string that I can make it save images as when they're uploaded, but it can't repeat, so it's different everytime. Kind of hard to explain it, so if you don't understand, ask a question and I'll answer it.

Link to comment
https://forums.phpfreaks.com/topic/129438-non-repetitive-random-string-generator/
Share on other sites

that is the code:)

$id will be set to a string you can use as the id that no files exist with that name.

 

Scott.

 

Okay thanks, one more questioned related to this. It's kind of a stupid question...

 

Say I'm allowing more than just .jpg images to be uploaded, and I want it to generate a unique ID if it exists for other image files, say png, is there a more efficient way of making it for all the extensions I want rather than repeating the code and replacing the extension with what I want each time?

I always like to keep an auto increment field when storing database entries, but I didn't want my filenames to follow the same scheme.

 

My solution was to convert to an alphanumeric, case-sensitive base. You may want to change this to case-insensitive for (some) OS X and Windows servers.

 

<pre><?php

function newBase( $i ) {
   static $chars = array(
      '0','1','2','3','4','5','6','7','8','9',
      'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
      'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
   );
   $base = count( $chars );
   
   $num = '';
   while( $i > 0 ) {
      $num .= $chars[ $i % $base ];
      $i = floor( $i / $base );
   }
   
   return strrev( $num );
}

$nums = array();
for( $i=0; $i<1000; $i++ )
   $nums[] = newBase( $i );

print_r( $nums );

?></pre>

The following is a quick script I wrote to do what the OP asked:

<?php
$chars = array_merge(range(0,9),range('A','Z'),range('a','z'));
$exts = array('jpg','gif','bmp');
function new_name($ary,$len=7) {
shuffle($ary);
$tfn = implode('',array_slice($ary,0,$len));
$test = glob('test_area/' . $tfn . '.{jpg,gif,bmp}',GLOB_BRACE);
while (count($test) > 0) {
	shuffle($ary);
	$tfn = implode('',array_slice($ary,0,$len));
	$test = glob('test_area/' . $tfn . '.{jpg,gif,bmp}',GLOB_BRACE);
}
return($tfn);	
}

//
//  test area
//
for ($i=0;$i<10;$i++) {
	$file_name = new_name($chars);
	$ext = $exts[array_rand($exts)];
	touch('test_area/' . $file_name . '.' . $ext);
	echo 'created test_area/' . $file_name . '.' . $ext . '<br>';
}
?>

 

Ken

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.