php使用扩展名重命名上传的文件

I have a php code as follows:

    $titleA = $_POST["title"];

if (in_array($_FILES["file"]["type"], $valid_mime_types)) {
    $destination = "bannerImages/" . $_FILES["file"]["name"];
    move_uploaded_file($_FILES["file"]["tmp_name"], $destination); }

I would like to change the file name with $titleA variable. But the extension will be the same. basically, filename will become what has been written for title. let me say, original file is first.jpeg second will be second.jpeg.

Hello if i understand you correctly you are having problem extracting extension information. you can always use pathinfo

Example

$extention = pathinfo("first.jpeg ",PATHINFO_EXTENSION);
$newName = "secound." . $extention ;
var_dump($newName);

Output

string 'secound.jpg' (length=11)

When using move_uploaded_file you get to pick the filename, so you can pick anything you want.

When you upload the file, its put into a temporary directory with a temporary name, move_uploaded_file() allows you to move that file and in that you need to set the name of the file as well.

basically you need extension of a uploaded file,

you have to get extension first, you have file

$_FILES["file"]["name"]

String position "strpos()" will get the dot point of a file

$idx = strpos($_FILES['file']['name'],'.');

After dot point you have to get file extension through;

$ext = substr($_FILES['file']['name'],$idx);

and finally change the file name as you desired,

$file_name = $titleA . $ext; 

Below is the code how I rename the file extension all from .png to .jpg

function replace_extension($filename, $new_extension) {
     $info = pathinfo($filename);
    $newname = $info['filename'] . '.' . $new_extension;
    rename('/home/image/'.$filename, '/home/image/'.$newname);
    return $newname;
}
$dir = '/home/image';
$files = scandir($dir);
//print_r($files);
foreach($files as $key => $file)
{
     echo replace_extension($file, 'jpg');
    echo "<br />";
}