I am trying to assign a unique file name to an uploaded file keeping the extension

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devilInDisguise
    New Member
    • Nov 2013
    • 2

    I am trying to assign a unique file name to an uploaded file keeping the extension

    I am new to PHP and I am trying to get this code to rename a file and save it in the subdirectory. But I need it to keep the extension. Please Help me!

    <?php
    $allowedExts = array("txt", "dxf","DXF" , "pdf", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);

    if ((($_FILES["file"]["type"] == "text/plain")
    || ($_FILES["file"]["type"] == "applicatio n/octet-stream")
    || ($_FILES["file"]["type"] == "applicatio n/pdf")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 2000000)
    && in_array($exten sion, $allowedExts))
    {
    if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
    echo ".tmpFiles: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists(". tmpFiles/" . $_FILES["file"]["name"]))
    {

    echo $_FILES["file"]["name"] . " already exists. ";
    }
    else
    {

    move_uploaded_f ile($_FILES["file"]["tmp_name"],
    ".tmpFiles/" . $_FILES["file"]["name"]);
    echo "Stored in: " . ".tmpFiles/" . $_FILES["file"]["name"];
    }
    }
    }
    else
    {
    echo "Invalid file";
    }

    include('list.p hp');

    ?>
    Thanks in advance for you help
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Describe the error please.

    Comment

    • devilInDisguise
      New Member
      • Nov 2013
      • 2

      #3
      I am not getting an error I just do not know how to do it. I have tried uniqid() but I am not sure where to put it and what syntax to use

      Comment

      Working...