我创建了一个具有上传功能的网站,并以 .xxx 扩展名存储在服务器中,并在每个文件上重命名,然后将原始扩展名和文件名记录到数据库中。
- 客户端 -> 上传 -> images.jpg
- 服务器 -> 保存 -> md5.xxx
- 数据库 -> 插入 -> images.jpg 和 md5.xxx
客户端 -> 下载 -> images.jpg (但没有重命名服务器中的文件或再次重命名为 md5)
如何使用php语言返回具有原始扩展名和名称文件的客户端?
你能帮我解决我的问题吗,谢谢,对不起我的英语。
您需要一个文件来为请求提供服务,downloadimage.php并将文件名传递给它,例如downloadimage.php?name=image.jpg. 取$_GET['name']并在数据库中查找文件名并提供服务。
然后使用重写使 url 漂亮
RewriteRule /somepath/(.*) /somepath/downloadimage.php?name=$1
所以你可以从/somepath/image.jpg
来自 PHP 文档标题的示例
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
downloadimage.php?name=image.jpg
<?php
// pseudo code
$md5_filename = get_md5_filename_from_database($_GET['name']);
// sending
header('Content-type: image/jpeg');
header('Content-Disposition: attachment; filename="'.$_GET['name'].'"');
readfile($md5_filename);
?>
现在您可以通过以下方式下载
www.yourdomain.com/some-path/downloadimage.php?name=image.jpg
或RewriteRule在 Apache 配置中使用(如 Musa 所说)来获取
www.yourdomain.com/some-path/image.jpg