www.webdeveloper.com
Results 1 to 3 of 3

Thread: Changing File Name When Uploaded

  1. #1
    Join Date
    Mar 2009
    Posts
    27

    Changing File Name When Uploaded

    Hello,

    So what I need is to add something to the code below that changes the file name to something random and uploads it as that random file name.

    This is what i have. What do i have to add to it to make it change the file name to something random?


    <?php
    // ==============
    // Configuration
    // ==============
    $uploaddir = "upload"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
    $allowed_ext = "swf, SWF"; // These are the allowed extensions of the files that are uploaded
    $max_size = "5000000"; // 50000 is the same as 50kb
    $max_height = ""; // This is in pixels - Leave this field empty if you don't want to upload images
    $max_width = ""; // This is in pixels - Leave this field empty if you don't want to upload images

    // Check Entension
    $extension = pathinfo($_FILES['file']['name']);
    $extension = $extension[extension];
    $allowed_paths = explode(", ", $allowed_ext);
    for($i = 0; $i < count($allowed_paths); $i++) {
    if ($allowed_paths[$i] == "$extension") {
    $ok = "1";
    }
    }

    // Check File Size
    if ($ok == "1") {
    if($_FILES['file']['size'] > $max_size)
    {
    print "Flash file size is too big! (maximum file size 4.5 megabytes)";
    exit;
    }

    // Check Height & Width
    if ($max_width && $max_height) {
    list($width, $height, $type, $w) =
    getimagesize($_FILES['file']['tmp_name']);
    if($width > $max_width || $height > $max_height)
    {
    print "Flash file height and/or width are too big!";
    exit;
    }
    }

    // The Upload Part
    if(is_uploaded_file($_FILES['file']['tmp_name']))
    {
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
    }
    print "Your files have been uploaded successfully! Thank you for submitting your flash game to GameRinger! We will contact you soon regarding how you would like to be recognized for your game submission.";
    } else {
    print "File extension is not permitted!";
    }
    ?>

    THANKS IN ADVANCE!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,617
    One way:
    PHP Code:
    $name "vid_".uniqid().".swf";
    move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir.'/'.$name); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2009
    Posts
    27

    Thank you!

    Thank you so much. that worked perfectly.

    Can you just help me a little more?

    I used your solution and it worked. I just want to add one thing on to it.

    You can view it here. i just posted it.

    http://webdeveloper.com/forum/showth...57#post1055257

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles