In my pregame, user need to upload an image, and system will rename to a serial number.
After I rename it the extension of the file is missing (.jpg/.png)
How can I get get the file extension
upload.php
$nric=$_SESSION['nric'];
if(isset($_POST['upload']))
{
$num=(rand(10,100));
$serialNumber = "TM".$userID.$num.$nric[7].$nric[8];
$_FILES['file']['name']=$serialNumber;
$file=strtoupper($_FILES['file']['name']);
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$folder="uploads/";
Use:
pathinfo($folder.$file,PATHINFO_EXTENSION)
With this code you can get the extension of the uploaded file.
If $_POST['upload'] is the original file name as I asked in the comments, then:
$ext = explode(".", $_POST['upload'])[1];
echo $ext; //gives jpg, gif or whatever the extension is
$nric=$_SESSION['nric'];
if(isset($_POST['upload'])) {
$file = explode(".", $_FILES["file"]["name"]);
$newfilename = "TM".$userID.$num.$nric[7].$nric[8] . '.' . end($file);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
}
OR
//Create a function that takes care of the randomization
function randStrGen($len){
$result = "";
$chars = "abcdefghijklmnopqrstuvwxyz0123456789$11"; //Characters allowed to produce a random string
$charArray = str_split($chars);
for($i = 0; $i < $len; $i++){
$randItem = array_rand($charArray);
$result .= "".$charArray[$randItem];
}
return $result;
}
$rand = randStrGen(24); //The amount of random characters needed
$nric=$_SESSION['nric']; //Session variable
if(isset($_POST['upload'])) {
$file = explode(".", $_FILES["file"]["name"]);
$newfilename = $rand . '.' . end($file);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename); // Change this to your server's image directory
}
OR BETTER STILL
if (isset($_POST['upload']))
{
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extension
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$filesize = $_FILES["file"]["size"];
$allowed_file_types = array('.jpg','.png','.gif','.jpeg');
if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
{
// Rename file
$newfilename = "TM" . md5($file_basename) . $userID. $num . $file_ext;
if (file_exists("upload/" . $newfilename))
{
// file already exists error
echo "You have already uploaded this file.";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename);
echo "File uploaded successfully.";
}
}
elseif (empty($file_basename))
{
// file selection error
echo "Please select a file to upload.";
}
elseif ($filesize > 200000)
{
// file size error
echo "The file you are trying to upload is too large.";
}
else
{
// file type error
echo "Only these file types are allowed for upload: " . implode(', ',$allowed_file_types);
unlink($_FILES["file"]["tmp_name"]);
}
}
What you basically need is //Change $file $file = $_FILES['file']['name']; $file_basename = substr($filename, 0, strripos($file, '.')); // get file extension
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.