I got this code from w3schools, and tweaked it based on this one: Rename an uploaded file with PHP but keep the extension
Please help, what do I do so that the file extension will not be .tmp When I upload it to a folder on the server.
<?php
$filename=$_FILES["file"]["tmp_name"];
$extension=end(explode(".", $filename));
$newfilename=$prod .".".$extension;
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000))
{
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("../img/user_uploaded/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($filename,
"../img/user_uploaded/" . $newfilename. $extension);
echo "Stored in: " . "../img/user_uploaded/" .$newfilename. $extension;
}
}
}
else
{
echo "Invalid file, File must be less than 100Kb in size with .jpg, .jpeg, or .gif file extension";
}
?>
$filename=$_FILES["file"]["tmp_name"];
$extension=end(explode(".", $filename));
$newfilename=$prod .".".$extension;
In the above code you are trying to get the temporary file's extension. Instead, you can take the extension of the original filename uploaded by the user.
$filename=$_FILES["file"]["name"];
$extension=end(explode(".", $filename));
$newfilename=$prod .".".$extension;
This will upload your file with the actual file extension.