Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change name of uploaded file without changing extension

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?

like image 507
Mirgorod Avatar asked Jun 20 '11 09:06

Mirgorod


People also ask

How do I Rename a file while uploading?

We can change file name by using FileUpload control's SaveAs method. SaveAs() method require to pass a parameter named filename. This parameter value is a string that specifies the full path of the location of the server on which to save the uploaded file.

Can I change any file type by changing the file name extension?

You can also do it by right-clicking on the unopened file and clicking on the “Rename” option. Simply change the extension to whatever file format you want and your computer will do the conversion work for you.

How do you correct a file name?

Rename a file in File Explorer or OneDrive Open File Explorer or OneDrive. Right-click the file and select Rename. Enter a new file name and press Enter.


1 Answers

$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
}
like image 129
Gaurav Avatar answered Oct 05 '22 23:10

Gaurav