Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help With Name Genorator

    How can i make it so that when someone uploads a files, the server will rename it to a random name?
    PHP Code:
    <html>
    <head>
    <title>ShotDev.Com Tutorial</title>
    </head>
    <body bgcolor="black" text="red">
    <center>
        <?php

            mysql_connect
    ("host","user","password") or die (mysql_error());
            
    mysql_select_db("DB - name");
        
    // i know my Data Base stuff, i just dont want anyone else to.
            
    for($i=1;$i<=(int)($_POST["hdnLine"]);$i++)
            {
                if(
    $_FILES["fileUpload".$i]["name"] != "")
                {
                    if(
    copy($_FILES["fileUpload".$i]["tmp_name"],"../gallery-images/".$_FILES["fileUpload".$i]["name"]))
                    {
                        
    $strSQL "INSERT INTO gallery ";
                        
    $strSQL .="(GalleryName,Picture) VALUES ('".$_POST["txtGalleryName".$i]."','".$_FILES["fileUpload".$i]["name"]."')";
                        
    mysql_query($strSQL);
                        echo 
    "<font color='white'>".$_FILES["fileUpload".$i]["name"]." </font>Has Been Uploaded!<br><hr><br>";
                        echo 
    "The Direct Link To The Image You Just Uploaded Is:<font color='green'><br><a href='http://smftesta.web44.net/gallery-images/".$_FILES["fileUpload".$i]["name"]."'>http://smftesta.web44.net/gallery-images/".$_FILES["fileUpload".$i]["name"]."</a></font><hr><br>";
                        echo 
    "The Forum Link To The Image You Just Uploaded Is:<font color='gold'><br>[IMG]http://smftesta.web44.net/gallery-images/".$_FILES["fileUpload".$i]["name"]."[/IMG]</font><hr><br>";
                    }
                }
            }

            echo 
    "<br><a href='http://pic-save.net16.net'>Home</a> | <a href='index.php'>Upload</a>";

            
    mysql_close();
        
    ?>
    </center>
    </body>
    </html>
    <!--- This file download from www.shotdev.com -->

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    68
    Thanks
    0
    Thanked 7 Times in 7 Posts
    This function can generate a random string for you:

    PHP Code:
    function random_string $length 20 )

        
    $chars "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ023456789"
        
    srand ( ( double ) microtime () * 1000000 );
        
    $i 0
        
    $string '' 
        while ( 
    $i <= $length 
        { 
            
    $num rand () % 33
            
    $tmp substr $chars$num); 
            
    $string $string $tmp
            
    $i++; 
        } 
        return 
    $string


  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    No need to seed the random number generator since about 10 years ago
    PHP Code:
    function random_string$length 32 )
    {

        
    $chars  array_mergerange'A''Z' ), range'a''z' ), range0) );
        
    $string '';

        for ( 
    $i 0$i $length$i++ )
        {
            
    $string .= $charsarray_rand$chars ) ];
        }

        return 
    $string;


    If these don't do it for you, there are about 218,000 random string code snippets out there on Google.

    Also, on the off chance a random string comes up twice, be sure to check if the file name already exists. If so, generate another. There are other ways to avoid this by using combinations of the current timestamp, microtime(), uniqid(), md5(), sha1(), etc, etc.
    PHP Code:
    // ensure the string is unique (any extension)
    do {
        
    $random_string random_string12 );
        
    $files         glob'../gallery-images/' $random_string '.*' );
    } while ( ! empty( 
    $files ) ); 
    PHP Code:
    // ensure the string is unique (specific extension)
    $ext pathinfo$_FILES["fileUpload".$i]["name"], PATHINFO_EXTENSION );

    do {
        
    $random_string random_string12 );
    } while ( 
    is_file'../gallery-images/' $random_string '.' $ext ) ); 
    Last edited by kbluhm; 12-14-2012 at 08:56 PM. Reason: typo: ! is_file() changed to is_file()


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •