Jump to content

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.

Edited by Digiboy
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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.