Welcome Guest, Not a member yet? Register   Sign In
Creating a uniqd id for users upload folder name
#1

Hi, I am just trying to get some ideas what best and whats not.

I was thinking when user creates a new account it also creates a upload folder for that user using its date_joined time()

The users folder would be in time() format


PHP Code:
$userdata $this->user_model->getuser($this->user_model->getuserid());

if (!
is_dir(FCPATH 'uploads/' $userdata['date_created'])) {
    mkdir(FCPATH 'uploads/' $userdata['date_created'], 0777true);


Out put

PHP Code:
C:\xampp\htdocs\project\uploads/1487475460 

Is that OK or if you have any other suggestions
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

When I create a user table, I assign a 'code' string to the user so I can refer to them in urls, something like users/view_profile/jfghdskjih so I would use that string (which is unique for each user and never changing) as the folder name.

I still use a piece of code suggested by Narf quite a long time ago for generating 'almost' guaranteed unique codes. I find it very useful and use it in some way on almost every site I do. Here it is in Narfs original form:

Code:
$code = bin2hex(pack('N', microtime(TRUE))).bin2hex(get_instance()->security->get_random_bytes(4));
https://forum.codeigniter.com/thread-634...#pid324949

I have a functions library full or things I find really handy that I transfer to all my sites and one of the functions in there is generate_code() which is the above function.

Just thought that might be of some use.
Reply
#3

(03-04-2017, 08:57 PM)PaulD Wrote: When I create a user table, I assign a 'code' string to the user so I can refer to them in urls, something like users/view_profile/jfghdskjih so I would use that string (which is unique for each user and never changing) as the folder name.

I still use a piece of code suggested by Narf quite a long time ago for generating 'almost' guaranteed unique codes. I find it very useful and use it in some way on almost every site I do. Here it is in Narfs original form:

Code:
$code = bin2hex(pack('N', microtime(TRUE))).bin2hex(get_instance()->security->get_random_bytes(4));
https://forum.codeigniter.com/thread-634...#pid324949

I have a functions library full or things I find really handy that I transfer to all my sites and one of the functions in there is generate_code() which is the above function.

Just thought that might be of some use.

FYI, get_instance()->security->get_random_bytes() binds you to the framework.

You can replace that with random_bytes() - native on PHP 7+ or via a compat package on older versions: https://github.com/paragonie/random_compat

When available, CI_Security::get_random_bytes() will just proxy to that function anyways.
Reply
#4

I made this for CI:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * ------------------------------------------------------------------------
 * Editor   : PhpStorm 2016.3
 * Date     : 1/2/2017
 * Time     : 2:36 PM
 * Authors  : Raymond L King Sr.
 * ------------------------------------------------------------------------
 * 
 * Class        UUID
 *
 * @project     starter
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2017 Pro Covers FX, LLC.
 * @license     http://www.procoversfx.com/license
 * ------------------------------------------------------------------------
 */

// version 4 uuid for namespace - can be generated online
//define('Uuid_Namespace', '1fb10795-5f81-454c-b857-2bc81956b0af');

/**
 * Class UUID
 *
 * Universally Unique Identifier Generator
 *
 * USAGE:
 *
 * $str = ''; // or Uuid_Namespace;
 *
 * $this->load->library(uuid);
 * 
 * echo $str.$this->uuid->guid_v4()."<br><br>";
 *
 * echo $str.$this->uuid->v3(Uuid_Namespace, md5(uniqid(mt_rand(), true)))."<br><br>"; // xxxxxxxx-xxxx-xxxx-xxxx-Out = Xxxxxxxxxxxx
 * echo $str.$this->uuid->v4()."<br><br>"; // xxxxxxxx-xxxx-xxxx-xxxx-Out = Xxxxxxxxxxxx
 * echo $str.$this->uuid->v5(Uuid_Namespace, md5(uniqid(mt_rand(), true)))."<br><br>"; // xxxxxxxx-xxxx-xxxx-xxxx-Out = Xxxxxxxxxxxx
 */

class UUID {

    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     * v3 ()
     * --------------------------------------------------------------------
     *
     * Version 3 UUIDs are named based.  They require a namespace (another
     * Valid UUID) and a value (the name).  Given the same namespace and
     * Name, the output is always the same.
     *
     * @param   Uuid   $namespace
     * @param   String $name
     * @return  bool|string
     */
    
public static function v3($namespace$name)
    {
        if (! 
self::isIdValid($namespace))
        {
            return 
false;
        }

        
// Get hexadecimal components of namespace
        
$nHex str_replace(array('-''{''}'), ''$namespace);

        
// Binary Value
        
$nStr '';

        
// Convert Namespace UUID to bits
        
for ($i 0$i strlen($nHex); $i += 2)
        {
            
$nStr .= chr(hexdec($nHex[$i] . $nHex[$i 1]));
        }

        
// Calculate hash value
        
$hash md5($nStr $name);

        return 
sprintf('%08S-04S-%%%04X-%04X-12s',

            
// 32 bits for "time_low"
            
substr($hash08),

            
// 16 bits for "time_mid"
            
substr($hash84),

            
/**
             * 16 bits for "time_hi_and_version",
             * Four most significant bits holds version number 3
             */
            
(hexdec(substr($hash124)) & 0X0Fff) | 0X3000,

            
/**
             * 16 bits, 8 bits for "clk_seq_hi_res", 8 bits for "clk_seq_low",
             * Two most significant bits holds zero and one for variant DCE1.1
             */
            
(hexdec(substr($hash164)) & 0X3Fff) | 0X8000,

            
// 48 bits for "node"
            
substr($hash2012)
        );
    }

    
/**
     * v4 ()
     * --------------------------------------------------------------------
     *
     * Version 4 UUIDs are pseudo-random.
     */
    
public static function v4()
    {
        return 
sprintf ('%04X%04X%04X04X-%04X-%%%04X-04X04X-%',

            
// 32 bits for "time_low"
         
   mt_rand(00Xffff), mt_rand(00Xffff),

            
// 16 bits for "time_mid"
         
   mt_rand(00Xffff),

            
// 16 bits for "time_hi_and_version",
            // Four most significant bits holds version number 4
         
   mt_rand(00X0Fff) | 0X4000,

            
// 16 bits, 8 bits for "clk_seq_hi_res", 8 bits for "clk_seq_low",
            // Two most significant bits holds zero and one for variant DCE1.1
         
   mt_rand(00X3Fff) | 0X8000,

            
// 48 bits for "node"
         
   mt_rand(00Xffff), mt_rand(00Xffff), mt_rand(00Xffff)
        );
    }

    
/**
     * guid_v4 ()
     * --------------------------------------------------------------------
     *
     * @return  string
     */
    
public static function guid_v4()
    {
        if (
function_exists('com_create_guid') === true)
        {
            return 
trim(com_create_guid(), '{}');
        }

        
$data openssl_random_pseudo_bytes(16);

        
// set version to 0100
        
$data[6] = chr(ord($data[6]) & 0x0f 0x40);

        
// set bits 6-7 to 10
        
$data[8] = chr(ord($data[8]) & 0x3f 0x80);
        
        return 
vsprintf('%s%s-%s-%s-%s-%s%s%s'str_split(bin2hex($data), 4));
    }

    
/**
     * v5 ()
     * --------------------------------------------------------------------
     *
     * Version 5 UUIDs are named based.  They require a namespace (another
     * Valid UUID) and a value (the name).  Given the same namespace and
     * Name, the output is always the same.
     *
     * @param   Uuid   $namespace
     * @param   String $name
     * @return  bool|string
     */
    
public static function v5($namespace$name)
    {
        if (! 
self::isIdValid($namespace))
        {
            return 
false;
        }

        
// Get hexadecimal components of namespace
        
$nHex str_replace(array('-''{''}'), ''$namespace);

        
// Binary Value
     
   $nStr '';

        
// Convert Namespace UUID to bits
        
for ($i 0$i strlen($nHex); $i += 2)
        {
            
$nStr .= chr(hexdec($nHex[$i] . $nHex[$i 1]));
        }

        
// Calculate hash value
        
$hash sha1($nStr$name);

        return 
sprintf('%08S-04S-%%%04X-%04X-12s',

            
// 32 bits for "time_low"
            
substr($hash08),

            
// 16 bits for "time_mid"
            
substr($hash84),

            
/**
             * 16 bits for "time_hi_and_version",
             * Four most significant bits holds version number 5
             */
            
(hexdec(substr($hash124)) & 0X0Fff) | 0X5000,

            
/**
             * 16 bits, 8 bits for "clk_seq_hi_res", 8 bits for "clk_seq_low",
             * Two most significant bits holds zero and one for variant DCE1.1
             */
            
(hexdec(substr($hash164)) & 0X3Fff) | 0X8000,

            
// 48 bits for "node"
            
substr($hash2012)
        );
    }

    
/**
     * isIdValid ()
     * --------------------------------------------------------------------
     *
     * @param   $uuid
     * @return  bool
     */
    
public static function isIdValid($uuid)
    {
        return 
preg_match ('/^\{?[0-9A-f]{8}\-?[0-9A-f]{4}\-?[0-9A-f]{4}\-?'.
         
                  '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i'$uuid) === 1;
    }

  // End of UUID Class.

/**
 * ------------------------------------------------------------------------
 * Filename: UUID.php
 * Location: ./application/libraries/UUID.php
 * ------------------------------------------------------------------------
 */ 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB