Jump to content

Renaming a an upload with md5


Digiboy

Recommended Posts

Hi guys,

 

I am building a back end for personal project, I need to upload image via back end and need to know how to rename file before upload to its name but with md5 encryption

 

$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES["image"]["tmp_name"],"../img/ga/" . $_FILES["image"]["name"]);

 

Thanks in advance to you all.

Link to comment
https://forums.phpfreaks.com/topic/273667-renaming-a-an-upload-with-md5/
Share on other sites

I have also made a CMS for my company, and I did a file-manager type page, where someone would upload the file, but the file would get rename, with a unique id. (to stop files being overwritten). this is the code I used for it:

 

<?php


if(!empty($_POST["upload_file"])){
$file = explode(".", $_FILES["file_upload"]["name"]);
$extension = strtolower($file[1]);

if(!empty($file[1])){
if(in_array($extension, array('jpg','jpeg', 'gif', 'png', 'bmp', 'doc', 'pdf', 'docx', 'mp4', 'zip', 'mov', 'mpg', 'wmv', '3gp'))){
$filepath = $_FILES["file_upload"]["tmp_name"];

$filename = uniqid().".".$file[1]; //THIS IS WHERE THE FILE IS RENAMED

$target = $_SERVER["DOCUMENT_ROOT"]."template/uploads/".$filename;
if(move_uploaded_file($filepath, $target)){

$document = str_replace("_", " ", $file[0]);
$document = str_replace("-", " ", $document);

$url = $GLOBALS["siteUrl"]."uploads/".$filename;

$putFileInfo = insert("files", "file_name, file_url, file_original, new_file", "'$document', '$url', '".$_FILES["file_upload"]["name"]."', '$filename'");

if($putFileInfo){
$filePass = "File was successfully uploaded.";
}else{
$fileErr = "File could not be uploaded: ".mysql_error();
}

}else{
$fileErr .= "Could not move file.";
}
}else{
$fileErr .= "That is not an accepted file extension for the $page.";
}
}elseif(empty($file[1])){
$fileErr = "No file selected.";
}
}

?>

  • 1 month later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.