2

I need to rename a file name while upload in codeigniter, by default it create a file name like sample.png, sample1.png while upload a same file again, but I need to give some authentication to the files uploading with random numbers like 1234_sample.png, 1232_sample.png.

Any way to do this. ?

Thanks in advance.

J.K.A.
  • 7,272
  • 25
  • 94
  • 163
Jothi Kannan
  • 3,320
  • 6
  • 40
  • 77

2 Answers2

3

In your controller BEFORE you run the actual upload code put the following:

$config['encrypt_name'] = TRUE;

As per the CodeIgniter documentation:

If set to TRUE the file name will be converted to a random encrypted string. This can be useful if you would like the file saved with a name that can not be discerned by the person uploading it.

This should be put before you run:

$this->load->library('upload', $config); // need to pass $config array
kittycat
  • 14,983
  • 9
  • 55
  • 80
  • it is working but it encrypted the file name, but i need to get the file name with random function like below posted by Ultimate – Jothi Kannan Jan 05 '13 at 13:10
  • 1
    @jothikannan, are you needing random filenames to prevent people from guessing filenames to access them, or random filenames to prevent the same file from possibly having the same name? The above will solve both situations and be much more random as the strings will be cryptographically generated. – kittycat Jan 05 '13 at 13:30
1

You haven't posted your code but still you can use PHP rand() function if you are facing the duplicate name issue:

You can do like this:

$file_name=rand(1,1000)."_sample.png";

OR

$file_name=time()."_sample.png";

Hope it'll help you.

J.K.A.
  • 7,272
  • 25
  • 94
  • 163
  • That helps, but does not ensure there will still not be a duplicate. Usually a timestamp is better for this. –  Jan 05 '13 at 11:27
  • Using time() does not make it random, but predictable. OP wants the randomness to be there for some low level form of authentication. – kittycat Jan 05 '13 at 11:32