rename, missing extention

Discussion in 'PHP' started by promotingspace.net, Aug 21, 2007.

  1. #1
    Hi
    I am still playing with an upload file script and having problem.
    I upload and copy the files but cannot hash its name.
    i use this code but the result is missing extention and so the file will not be opened
    what is the solution?
    thanks
    <?php
     $name= $_FILES["file"]["name"];
          $ar=explode(".",$name);
          $ext=$ar[1];
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "uploads/" . $_FILES["file"]["name"]);
          $newname=md5(time());
          rename('uploads/'. $_FILES["file"]["name"],'uploads/'.$newname.$ext);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
    ?>
    PHP:

     
    promotingspace.net, Aug 21, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $newname=md5(time()) . '.' . end(explode('.', $_FILES['file']['name']));
    
    PHP:
    Don't rely on the extension being $ar[1], because the file can have a lot of dots, and faking the extension would be super easy.

    And remove this line:
    
    rename('uploads/'. $_FILES["file"]["name"],'uploads/'.$newname.$ext);
    
    PHP:
    Put the new name directly in the second parameter of move_uploaded_file().
     
    nico_swd, Aug 22, 2007 IP