如何以原始名称而不是唯一名称下载文件? [英] How to download file with its original name instead of unique name?
问题描述
当前我的文件上传进度如下:
Currently my file uploading progress is following:
- 用户选择要上传的文件.
-
然后使用以下PHP代码将用户上传的文件重命名为唯一名称:
- User select a file to upload.
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];
之后,我将这个唯一的文件上传到我的"upload_doc/"目录中.
After that I just upload this unique file to my 'upload_doc/' directory.
现在我的脚本中有一个选项可以下载此上传的文件.要下载,我只是从"updload_doc/"目录中调用此unique
文件名并下载.
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.
是否可以用原始名称而不是unique
文件名下载此文件?
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?
我的文件下载系统:
这是用于下载的按钮:
<a target="_self" href="download.php?filename=<?php echo $rselectfile1['file_name']; ?>"><input type="button" name="download" value="Download" class="submit"/></a>
这是download.php文件
<?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" );
?>
推荐答案
我只是在文件名前面加上哈希值,而不是替换它.这样可以保留原始文件名.因此您的代码如下所示:
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
然后将文件名设置为:
header('Content-Disposition: attachment; filename="' . $file_name1 . '"');
这篇关于如何以原始名称而不是唯一名称下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!