Digiboy Posted January 26, 2013 Share Posted January 26, 2013 (edited) 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 January 26, 2013 by Digiboy Quote Link to comment https://forums.phpfreaks.com/topic/273667-renaming-a-an-upload-with-md5/ Share on other sites More sharing options...
White_Lily Posted January 26, 2013 Share Posted January 26, 2013 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."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/273667-renaming-a-an-upload-with-md5/#findComment-1408379 Share on other sites More sharing options...
shlumph Posted January 26, 2013 Share Posted January 26, 2013 (edited) Rename it when you move it. move_uploaded_file($_FILES["image"]["tmp_name"],"../img/ga/newFileName.ext"); Edited January 26, 2013 by shlumph Quote Link to comment https://forums.phpfreaks.com/topic/273667-renaming-a-an-upload-with-md5/#findComment-1408380 Share on other sites More sharing options...
Digiboy Posted February 28, 2013 Author Share Posted February 28, 2013 Thanks all of you. Quote Link to comment https://forums.phpfreaks.com/topic/273667-renaming-a-an-upload-with-md5/#findComment-1415499 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.