php - How to download file with its original name instead of unique name? -


currently file uploading progress following:

  1. user select file upload.
  2. then rename user uploaded file unique name following php code:

    $ext = explode('.', basename( $_files['file1']['name'])); $file_name1 = md5(uniqid()) . "." . $ext[count($ext)-1];  
  3. after upload unique file 'upload_doc/' directory.

now there option in script download uploaded file. download, i'm calling unique file name 'updload_doc/' directory , download it.

now it's showing regenerated file name when download file.

is there way download file original name instead of unique file name?

note: can upload original file directory happened if user uploads same file? can guys tell trick this?

my file downloading system :

this button download:

<a target="_self" href="download.php?filename=<?php echo $rselectfile1['file_name']; ?>"><input type="button" name="download" value="download" class="submit"/></a> 

this download.php file

<?php @session_start(); //require_once("config.php"); if(!isset($_session['front_username']) && isset($_session['front_username']) == "" &&     !isset($_session['front_password']) && isset($_session['front_password']) == "" &&      !isset($_session['user_id']) && isset($_session['user_id']) == "") {     header("location:login.php");        exit(); }   $file_get = $_get['filename'];  $tmp = explode(".",$file_get); switch ($tmp[count($tmp)-1]) {   case "pdf": $ctype="application/pdf"; break;   case "exe": $ctype="application/octet-stream"; break;   case "zip": $ctype="application/zip"; break;   case "docx":   case "doc": $ctype="application/msword"; break;   case "csv":   case "xls":   case "xlsx": $ctype="application/vnd.ms-excel"; break;   case "ppt": $ctype="application/vnd.ms-powerpoint"; break;   case "gif": $ctype="image/gif"; break;   case "png": $ctype="image/png"; break;   case "jpeg":   case "jpg": $ctype="image/jpg"; break;   case "tif":   case "tiff": $ctype="image/tiff"; break;   case "psd": $ctype="image/psd"; break;   case "bmp": $ctype="image/bmp"; break;   case "ico": $ctype="image/vnd.microsoft.icon"; break;   default: $ctype="application/force-download"; }  header("pragma: public"); // required header("expires: 0"); header("cache-control: must-revalidate, post-check=0, pre-check=0"); header("cache-control: private",false); // required browsers header("content-type: $ctype"); header("content-disposition: attachment; filename=\"".$file_get."\";" ); header("content-transfer-encoding: binary"); header("content-length: ".$fsize); ob_clean(); flush(); readfile("upload_doc/$file_get" );   ?> 

i prefix filename hash instead of replacing it. way original filename preserved. code like:

$file_name1 = md5(uniqid()) . '-' . $_files['file1']['name']; // example: b4b34d95b44ea3ebaa125508fd51dd44-image.jpg 

on download, remove hash filename:

$file_name1 = substr($file_name1, 33); // example: image.jpg 

and set filename like:

header('content-disposition: attachment; filename="' . $file_name1 . '"'); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -