2012-08-07 104 views
0

我发现这个脚本的作用像一个魅力,但我的下载文件现在有名称中的网址。我该如何删除它?如何从下载文件中删除网址

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); 

感谢所有

回答

3

在PHP中,请执行下列操作:

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

将从文件路径中删除URI的其余部分,并留下你的文件名。

+0

Wauw,那很快!绝对的辉煌。非常感谢格雷格:) – Jimmy 2012-08-07 22:41:52

+0

没问题。确保接受答案,如果它解决了你的问题。 – gcochard 2012-08-07 22:55:42

+0

啊,好的。它有用吗?我的第一篇文章在这里。 – Jimmy 2012-08-07 23:41:36