Jimmy
Jimmy

Reputation: 3

How to remove url from downloading file

I've found this script which works like a charm but my downloaded files has now the url in the name. How can I remove that?

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

Thanks all

Upvotes: 0

Views: 600

Answers (1)

gcochard
gcochard

Reputation: 11734

In the php, do the following:

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

That will remove the rest of the URI from the file path, and leave you with the filename.

Upvotes: 3

Related Questions