Welcome Guest, Not a member yet? Register   Sign In
Is File Upload class creates unique file name when encrypt_name set true?
#1

Is File Uploading class in code ignter creates unique file name when encrypt_name set true?

       
PHP Code:
$config['upload_path'] = $abs_path   
        $config
['allowed_types'] = 'doc|docx|pdf';
 
       $config['max_size'] = '5120';
 
       $config['remove_spaces'] = TRUE       
        $config
['encrypt_name'] = TRUE;
 
       $this->load->library('upload'$config); 
Reply
#2

It's in the User's Guide.

encrypt_name

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 10-30-2018, 08:56 AM by karthik_code.)

(10-30-2018, 03:07 AM)InsiteFX Wrote: It's in the User's Guide.

encrypt_name

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.
Thanks for the quick reply.
I already read the guide. My question is, whether it will be a unique name. Because, I am storing all user uploads in a single folder, hence it should not be overwritten at any time.
Also, I could not able to find a way to prefix a unique id to the random filename.
If I manually set a unique file name, unable to determine the original file's extension.
Reply
#4

encrypt_name does not guarantee a unique file name. That said, the chance of a name collision is incredibly small. But a small chance is not the same as no chance.

You might be interested to know that do_upload() calls set_filename() and set_filename() will append a number to the end of the filename to avoid overwriting a pre-existing file. This happens for any file upload encrypted name or not.
Reply
#5

(This post was last modified: 10-30-2018, 11:09 AM by jreklund.)

Personally I'm using a UUID version 4 generator for my file names and all ID (instead of auto increment).

PHP Code:
if ( ! function_exists('UUIDv4'))
{
    
/**
     * Generates a random UUID using the secure RNG.
     *
     * Returns Version 4 UUID format: xxxxxxxx-xxxx-4xxx-Yxxx-xxxxxxxxxxxx where x is
     * any random hex digit and Y is a random choice from 8, 9, a, or b.
     *
     * @return string the UUID
     */
    
function UUIDv4()
    {
        
$bytes random_bytes(16);
        
$bytes[6] = chr((ord($bytes[6]) & 0x0f) | 0x40);
        
$bytes[8] = chr((ord($bytes[8]) & 0x3f) | 0x80);
        
$uuid vsprintf('%s%s-%s-%s-%s-%s%s%s'str_split(bin2hex($bytes), 4));
        return 
$uuid;
    }


https://en.wikipedia.org/wiki/Universall...Collisions
Reply
#6

(10-30-2018, 09:41 AM)dave friend Wrote: You might be interested to know that do_upload() calls set_filename() and set_filename() will append a number to the end of the filename to avoid overwriting a pre-existing file. This happens for any file upload encrypted name or not.
This is the function i want.
Reply
#7

Thanks @jreklund & @Dave friend, I finally ended with prefixing auto-increament id along with uuid to the uploaded file name. I worried about setting the original file's extension with filename, but CodeIgniter automatically preserves the original file's extension even I didn't mention it in $config['file_name'] parameter.
Reply
#8

You need a .htaccess file on your root folder 

like below code

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
Reply




Theme © iAndrew 2016 - Forum software by © MyBB