php 如何在不更改扩展名的情况下更改上传文件的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6409261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:17:45  来源:igfitidea点击:

How to change name of uploaded file without changing extension

phpfilefunctionupload

提问by Mirgorod

i'd like to change the name of uploaded file to md5(file_name).ext, where ext is extension of uploaded file. Is there any function which can help me to do it?

我想将上传文件的名称更改为 md5(file_name).ext,其中 ext 是上传文件的扩展名。有什么功能可以帮助我做到这一点吗?

回答by Gaurav

$filename  = basename($_FILES['file']['name']);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new       = md5($filename).'.'.$extension;

if (move_uploaded_file($_FILES['file']['tmp_name'], "/path/{$new}")) 
{
   // other code
}

回答by Varun Mittal

Use this function to change the file name to md5 with the same extension

使用此函数将文件名更改为具有相同扩展名的 md5

function convert_filename_to_md5($filename) { 
    $filename_parts = explode('.',$filename);
    $count = count($filename_parts);
    if($count> 1) {
        $ext = $filename_parts[$count-1];
        unset($filename_parts[$count-1]);
        $filename_to_md5 =  implode('.',$filename_parts);
        $newName = md5($filename_to_md5). '.' . $ext ;
    } else {
        $newName = md5($filename);
    }        
    return $newName;
}

回答by shameem

   <?php
    $filename   = $_FILES['file']['name'];
    $extension  = pathinfo($filename, PATHINFO_EXTENSION);
    $new        = rand(0000,9999);
    $newfilename=$new.$filename.$extension;

    if (move_uploaded_file($_FILES['file']['tmp_name'],$newfilename)) 
    {
      //advanced code 
    }
    ?>

回答by Sanjeev Chauhan

Find below php code to get file extension and change file name

找到下面的php代码以获取文件扩展名并更改文件名

<?php
if(isset($_FILES['upload_Image']['name']) && $_FILES['upload_Image']['name']!=='') {
    $ext = substr($_FILES['upload_Image']['name'], strpos($_FILES['upload_Image']['name'],'.'), strlen($_FILES['upload_Image']['name'])-1);     
    $imageName = time().$ext;
    $normalDestination = "Photos/Orignal/" . $imageName;
    move_uploaded_file($_FILES['upload_Image']['tmp_name'], $normalDestination);
}
?>

回答by Usf Ahmed Subehi

This one work

这一项工作

<?php

// Your file name you are uploading 
$file_name = $HTTP_POST_FILES['ufile']['name'];

// random 4 digit to add to our file name 
// some people use date and time in stead of random digit 
$random_digit=rand(0000,9999);

//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables

$new_file_name=$random_digit.$file_name;

//set where you want to store files
//in this example we keep file in folder upload 
//$new_file_name = new upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "upload/".$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>"; 

//$new_file_name = new file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$new_file_name."<BR/>"; 
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>"; 
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>"; 
}
else
{
echo "Error";
}
}
?>