Random Number after Name

Discussion in 'PHP' started by er1cw, Sep 15, 2006.

  1. #1
    Hi, I am rewriting a file uploading script in php. I would like to add a string of random number after the file name. May i know how can it be done ?

    example: $filename=mypicture_jpg(random number)

    Below is my current code:
    $filename = $_FILES['upfile']['name'];
    $filename= str_replace(".","_",$filename);
    PHP:
     
    er1cw, Sep 15, 2006 IP
  2. VONRAT

    VONRAT Banned

    Messages:
    181
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    int rand ( [int min, int max])


    If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use rand (5, 15).


    so basically you can just do something like


    
    $filename = $filename.rand(1000,5000);
    
    Code (markup):
    :)
     
    VONRAT, Sep 15, 2006 IP
  3. er1cw

    er1cw Peon

    Messages:
    114
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. That's exactly what i want. I have another problem now, sorry i am new in php = )

    i cant change the "space" into "_" ? Anything wrong this these line ?

    $filename = $_FILES['upfile']['name'];
    $filename= str_replace(" ","_",$filename);
    $filename= str_replace(".","_",$filename);
    $filename = $filename.rand(1000,9999);
    PHP:
     
    er1cw, Sep 15, 2006 IP
  4. er1cw

    er1cw Peon

    Messages:
    114
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Got it fixed. I just change the line to

    $filename= str_replace(array(" ","."),"_",$filename);
    
    PHP:
    Stupid me.

    Thanks!
     
    er1cw, Sep 15, 2006 IP