how can i make it random?

Discussion in 'PHP' started by izlik, Apr 9, 2015.

  1. #1
    Hi

    I have this code that saves images. How can i make it so that instead of the original name it just randoms a number? so instead of "name.extension" it will be "randomnumber.extension" ?

    $tmp = download_url( $img );
    
                    preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $img, $matches);
                  
                    $newfn = str_replace(array("%2B", "%52", "%20", "%5"), "B", basename($matches[0]));
                  
                    $oofnm = basename($matches[0]);
                  
                    if($newfn != $oofnm) {              
                  
                        $newfn2 = str_replace(array(".jpg", ".png", ".gif"), "", $newfn);
    
                        $tmppath = pathinfo( $tmp );                                                        // extract path parts
                        $newpth = $tmppath['dirname'] . "/". $newfn2 . "." . $tmppath['extension']; 
    
                        rename($tmp, $newpth);                                                                 // renames temp file on server
                        $tmp = $newpth;                 
                    }
    
                    $file_array['name'] = $newfn;
                    $file_array['tmp_name'] = $tmp;
    
                    // If error storing temporarily, unlink
                    if ( is_wp_error( $tmp ) ) {
                        @unlink($file_array['tmp_name']);
                        $file_array['tmp_name'] = '';
                        continue;
                    }
    PHP:
     
    izlik, Apr 9, 2015 IP
  2. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #2
    Use rand() or any other rand function.
     
    webshore88, Apr 13, 2015 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    okay but where? i cant for my life find where in the code i should do it because whatever i try breaks it.
     
    izlik, Apr 13, 2015 IP
  4. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #4
    Here is the function that genarate random string, I use it myself.

    
    function generateRand($length){
            $rawSalt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            $rawSalt = str_shuffle($rawSalt);
            return substr($rawSalt, 0, $length);
        }
    
    PHP:
    Okay now user it this way

    first paste above function at the bottom of the php page, then go to line 14 the are variable "$newfn2", you need to change its value.

    paste this code just below line 13.

    $newfn2 = generateRand(8);
    PHP:
    and I hope your problem will be fixed by above solution.

    If there is any query, please let me know.
     
    webshore88, Apr 13, 2015 IP