0

So users are able to upload a profile picture for a website i'm creating - the script below works fine and saves the files as intended, however i want to re-name the file upon being uploaded to match the username of the user - i almost had it working, but it saved the file without the file extension.

session_start();
$filename = $_SESSION['username'];


$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts)) {
  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";
}

What i tried was change

move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);

To

move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $filename;

Which lead to me having the correct filename, just without an extension, which will be dictated obviously by whichever filetype they upload.

Nikki Mather
  • 1,118
  • 3
  • 17
  • 33

2 Answers2

0

It looks like you are looking for pathinfo to find the extension, as this answer suggests.

$info = pathinfo($_FILES["file"]["name"]);

move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/$filename." . $info['extension']);
Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • Can't seem to get this to work, get this error; `Parse error: syntax error, unexpected ';' in /home/maver08/public_html/design/inc/upload-file.php on line 31` and line 31 is `move_uploaded_file($_FILES["file"]["tmp_name"], "upload/$filename." . $info['extension'];` – Nikki Mather Jul 22 '14 at 23:45
  • @NikkiMather, I was missing a semicolon. See the update. – merlin2011 Jul 22 '14 at 23:49
  • Yeah i noticed that and added it before i posted back and it wasn't the cause of the issue. The issue seems to lie directly with the move_upload_file part - i can't work it out to save my life. – Nikki Mather Jul 22 '14 at 23:54
  • Close your parentheses. – CrypticStorm Jul 22 '14 at 23:58
  • @CrypticStorm, Thanks. I am running on very low sleep today so I should probably stop answering questions. :P – merlin2011 Jul 22 '14 at 23:58
  • Thanks, that stops the error. The file is now saving as Username. and doesn't have a file extension i.e. .png etc, is there anything obvious i need to do to get this working? – Nikki Mather Jul 23 '14 at 00:03
  • @NikkiMather, I just replaced `"tmp_name"` with `"name"` in the first line. Let me know if that does it for you. – merlin2011 Jul 23 '14 at 00:05
0

You could get the PATHINFO_EXTENSION.

$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);

then add it to the directory in which you want to save the file.

Carlos
  • 57
  • 1
  • 16