PHP File Upload - Rename Uploaded File

Discussion in 'PHP' started by Adam Abadilla, Aug 30, 2010.

  1. #1
    Hey guys, my site is using a very basic upload script. Here's what I need it to do..
    I need it to rename the uploaded file to "1.mp3" and overwrite if it already exists. I've been trying out a few things, but they don't seem to be working. I definitely appreciate any help you guys can give me! thanks! :D

    This is my code that I'm using:
    
    <?php
    if ((($_FILES["file"]["type"] == "music/mp3")
    && ($_FILES["file"]["size"] < 20000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_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("upload/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "upload/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?> 
    
    PHP:
     
    Adam Abadilla, Aug 30, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Replace all occurences of (do a search and replace):

    "upload/" . $_FILES["file"]["name"]
    PHP:
    With:

    "upload/1.mp3"
    PHP:
     
    danx10, Aug 30, 2010 IP
  3. Adam Abadilla

    Adam Abadilla Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #3
    Hey thanks for you fast response! It almost seems to work except that I get a syntax error on Line 4

    <?php
    if ((($_FILES["file"]["type"] == "mp3")
    && ($_FILES["file"]["size"] < 20000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_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("upload/1.mp3"))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "upload/1.mp3");
          echo "Stored in: " . "upload/1.mp3";
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?> 
    PHP:
    Any idea why?
     
    Adam Abadilla, Aug 30, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    You've got an unclosed parentheses on your first if statement...here's it simplified:

    <?php
    if ($_FILES["file"]["type"] == "mp3" && $_FILES["file"]["size"] < 20000) {
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        } else {
            echo "Upload: " . $_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("upload/1.mp3")) {
                echo $_FILES["file"]["name"] . " already exists. ";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"], "upload/1.mp3");
                echo "Stored in: " . "upload/1.mp3";
            }
        }
    } else {
        echo "Invalid file";
    }
    ?>
    PHP:
     
    danx10, Aug 30, 2010 IP
  5. dfsweb

    dfsweb Active Member

    Messages:
    1,587
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    88
    #5
    The above script will throw an error if 1.mp3 already exists whereas according to the first post, the script should just overwrite the file even if it exists.

    To do this, replace this block of code:
    
    if (file_exists("upload/1.mp3")) {
                echo $_FILES["file"]["name"] . " already exists. ";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"], "upload/1.mp3");
                echo "Stored in: " . "upload/1.mp3";
            }
    
    PHP:
    With simply this one line:
    
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/1.mp3");
    
    PHP:
     
    dfsweb, Aug 31, 2010 IP
  6. Adam Abadilla

    Adam Abadilla Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #6
    Hey I want to thank both of you for your responses... however... I've been getting an error "Invalid File". I've tried messing around with the code just a bit, but its still not working. Here is the page that it is actually on http://tundro.us/upload/ and here is the code I'm using right now:

    
    <?php
    if ($_FILES["file"]["type"] == "mp3" && $_FILES["file"]["size"] < 20000) {
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        } else {
            echo "Upload: " . $_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 />";
    	move_uploaded_file($_FILES["file"]["tmp_name"], "upload/1.mp3");
        }
    } else {
        echo "Invalid file";
    }
    ?>
    PHP:
    Thanks again
     
    Adam Abadilla, Aug 31, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    change:

    $_FILES["file"]["type"] == "mp3"
    PHP:
    to:

    pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION) == 'mp3'
    PHP:
    mp3 is not a valid mime type, secondly you shouldn't rely on it for validation.
     
    danx10, Aug 31, 2010 IP