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

我的梦境 提交于 2019-12-25 03:39:56

问题


Currently my file uploading progress is following:

  1. User select a file to upload.
  2. Then I rename user uploaded file to unique name with following PHP code:

    $ext = explode('.', basename( $_FILES['file1']['name']));
    $file_name1 = md5(uniqid()) . "." . $ext[count($ext)-1]; 
    
  3. After that I just upload this unique file to my 'upload_doc/' directory.

Now there are a option in my script to download this uploaded file. To download, I'm just calling this unique file name from 'updload_doc/' directory and download it.

Now it's showing my regenerated file name when I download a file.

Is there any way to download this file with its original name instead of unique file name?

Note: I can upload original file to a directory but what happened if the user uploads the same file? Can you guys tell what is the trick to do this?

My file downloading system :

This is a button for Download:

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

This is 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 for certain 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" );


?>

回答1:


I would just prefix the filename with the hash instead of replacing it. This way original filename is preserved. So your code would look like:

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

On download, you could just remove this hash from the filename:

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

And then set the filename like:

header('Content-Disposition: attachment; filename="' . $file_name1 . '"');



回答2:


That's easy. Just save both original and unique filenames to the DB when the file is uploaded and then in the download script select the row from the database using the unique file name and set its name to the original name.

Original file name is stored it his variable in the uploading script:

$_FILES['file_input_tag_name']['name']

To set custom filename of the downloaded file, just set the right header field:

header('Content-Disposition: attachment; filename="' . $original_filename . '"');


来源:https://stackoverflow.com/questions/24457485/how-to-download-file-with-its-original-name-instead-of-unique-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!