0

i need to upload images with md5/uniqid name.

PHP:

$filenamekey = md5(uniqid($_FILES["myfile"]["name"], true)); 

move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$filenamekey);

This move image into folder with md5 name but upload files without any image extension/type. how can I fix this?

Pic?

enter image description here

Alex.DX
  • 141
  • 1
  • 4
  • 14

3 Answers3

1

Can you try this, You need to add the file extension in your filename, since you have converted your filename as md5 encoded, even it encoded the extension as well. In that case, you need to add the file extension.

$Filepath = $_FILES['myfile']['name'];
$filenamekey = md5(uniqid($Filepath, true));     
$Fileext = pathinfo($Filepath, PATHINFO_EXTENSION);
$filenamekey = $filenamekey.'.'.$Fileext;
Krish R
  • 22,583
  • 7
  • 50
  • 59
1

Get the file extension and add it to your $filenamekey.

$extension = pathinfo($_FILES["myfile"]["name"], PATHINFO_EXTENSION);
$filenamekey .= "." . $extension;
jszobody
  • 28,495
  • 6
  • 61
  • 72
0

You can do this:

$filenamekey = md5(uniqid($_FILES["myfile"]["name"], true));

// find the original extension and append it to the filename
$filenamekey .= "." . pathinfo($_FILES["myfile"]["name"], PATHINFO_EXTENSION);

move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$filenamekey);
Martin Majer
  • 3,274
  • 4
  • 23
  • 36