如何从下载文件中删除网址

时间:2012-08-07 22:32:09

标签: php jquery url

我发现这个脚本的功能就像一个魅力,但我下载的文件现在名字中有url。我该如何删除?

The html:
<a class="download_link" href="catalogue/CP-Greengo-Filtertips-30027.jpg"><img src="CP-Greengo-Filtertips-30027.jpg" width=100 alt=image></a>

The jQuery:
$('a.download_link').on('click', function (event) {
event.preventDefault(); //prevent the normal click action from occuring
window.location = 'php/filedownload.php?file=' + encodeURIComponent(this.href);});

The php:
$file = $_GET['file'];
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");//notice this content-type, it will force a download since browsers think that's what they should do with .exe files
header("Content-disposition: attachment; filename= ".$file."");
readfile($file);

全部谢谢

1 个答案:

答案 0 :(得分:3)

在php中,执行以下操作:

$pathParts = explode('/',$file);
$fileName  = $pathParts[count($pathParts)-1];
header("Content-disposition: attachment; filename=".$fileName);

这将从文件路径中删除其余的URI,并为您保留文件名。