开发者

How to change name of uploaded file without changing extension

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


$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
}


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

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;
}


   <?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 
    }
    ?>


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

<?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);
}
?>


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";
}
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜