#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Exclamation Uploading file with automated random genertaed names


    Hi,
    I am a beginner so I need your guys help in the following simple code i.e,


    $uploaddir = "profilepictures";
    if(is_uploaded_file($_FILES['file']['tmp_name']))

    {
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
    print "Your picture has been uploaded successfully!";
    }


    i want to use the 'rand' command but i dont know where to add that in the above code so kindly please help me,thanks...
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    You need to replace $_FILES['file']['name'] with something that generates a random file name. For example, the uniqid() function.
    PHP Code:
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'uniqid() . '.jpg'); 
    Although ideally you need to run some checks to make sure the file doesn't already exist in the directory, otherwise you'll overwrite it.

    If you change the '.jpeg' part to anything other than a static extension then you need to perform validation of the extension against a whitelist.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Smile


    Originally Posted by E-Oreo
    You need to replace $_FILES['file']['name'] with something that generates a random file name. For example, the uniqid() function.
    PHP Code:
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'uniqid() . '.jpg'); 
    Although ideally you need to run some checks to make sure the file doesn't already exist in the directory, otherwise you'll overwrite it.

    If you change the '.jpeg' part to anything other than a static extension then you need to perform validation of the extension against a whitelist.
    Hey thanks alot buddy, i really appreciate it!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Exclamation


    hey can you also tell me how to insert the uniqid generated into database simultaneously,thanks

IMN logo majestic logo threadwatch logo seochat tools logo