cannot open downloaded file having '&' symbol in filename - php

I want to download files uploaded to the server.I have the filenames as links and I need to download the file directly when the user click it.I have succeeded in that.The issue now I face is I cant open the files containing amp symbols in their filename,eventhough it is downloaded. eg: &,test.doc(filename containing other special symbols can be downloaded and opened properly.)
My php script is like this:
<script>
function downloadme(file){
var link = "download.php?file=" + file;
location.replace(link);
}
</script>
<a title="Click to download" href="javascript:downloadme()">filename</a>
download.php
<?php
$path = $_GET['file'];
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"" . basename($path . $uri[1]) . "\"" );
#readfile($path);
?>

Here's what I did
download.php:
<?php
$path = $_GET['file'];
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"" . basename($path . $uri[1]) . "\"" );
#readfile($_GET['file']);
?>
html file:
<script>
function downloadme(file){
var link = "download.php?file=" + escape(file);
location.replace(link);
}
</script>
<a title="Click to download" href="javascript:downloadme('test&12.doc')">filename</a>

Related

How to download wav or mp3 file from url PHP? Url playing my clip after authentication

I used the following code to play my url and its working fine.
<video controls="" autoplay="" name="media"><source src="url.wav" type="audio/x-wav"></video>
And tried the following code for download
header('Content-Type: audio/x-wav');
$file = "url.wav";
if( !file_exists($file) ) die("File not found");
header("Content-Disposition: attachment; filename=" . basename($file) . "");
header("Content-Length: " . filesize($file));
header('Content-Type: audio/x-wav, audio/wav');
readfile($file);
I am using all code in view file of Yii framework.
In Yii, you can do this like below in your controller:
return Yii::app()->request->sendFile($file, #file_get_contents($file),'audio/x-wav');

php download not working for &

I have this php function to download files from my public_html.
function download($file){
$dir = "RepList/";
$path = $dir.$file;
if(!file_exists($path))
{
die("Error : The file does not exists !");
}else{
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Typr: application/w3g");
header("Content-Transfer-Encoding: binary");
readfile($path);
}
}
if (isset($_GET['download']))
{
if(!empty($_GET['download']))
{
$file = $_GET['download'];
download($file);
}
}
For download I use something like:
<a class="download" id="download'.$i.'_" href="../download.php?download='.$cc->EList[$i]->getEName().'" target="_blank">Download</a>
If I have "&" symbol in my file name, I will receive "Error : The file does not exists !".
I worked on this like 1 year ago and now I want to fix the error.
Any advice? Something easy & fast.
You should probably use urlencode() function and change creating HTML link to:
<a class="download" id="download'.$i.'_" href="../download.php?download='.urlencode($cc->EList[$i]->getEName()).'" target="_blank">Download</a>
otherwise & character is treated as variables separator in url

Error in downloading file PHP

I have a link which shows the filename to download.When a user clicks it,it needs to get downloaded.The file gets downloaded but it contains only 0 KB.In console it shows
Resource interpreted as Document but transferred with MIME type application/force-download: "../download.php?file=filename"
My code is like this:
<a href="download.php?file=user_uploads/'.$_path['uploads'].
'logo_images/'.$row['FileName'].'" title="Click to download">'.$row['FileName'].'</a>
The download.php is like this:
<?php
$path = str_replace('/download.php?file=','',$_SERVER['REQUEST_URI']);
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"" . basename($path . $uri[1]) . "\"" );
#readfile($path);
?>
Thanks in advance.I have checked the path of the file also.
try
Click
download.php
<?php
$path = 'yourpath'.$_GET['file'];
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".$_GET['file'] );
#readfile($path);
?>
file.txt - change with your file name
I had similar issue while downloading my file. I used this code for download.php:
<?php
$path = $_REQUEST['path'];
#setting headers
header('Content-Description: File Transfer');
header('Cache-Control: public');
header('Content-Type: application/zip');
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename='. basename($path));
header('Content-Length: '.filesize($path));
ob_clean(); #THIS!
flush();
readfile($path);
exit;
?>
and my link was:
Download Pack
Hope it helps.

Renaming Download File Using Header in PHP

i have a link pointing to a music file on my site, now the name of the file was hashed when it was uploaded so i want to use the original filename which i have stored in my database, I did some research and found this new 'download' attribute for the 'a' tag but that only works in later versions of firefox and chrome, it doesn't work in ie and also doesn't work with the download manager i use so i checked online and found out about headers which i then implemented. I now get the filename changed allright but the music file keeps on getting saved as a '11.35kb' filesize no matter the music file i try to download. This is my code:
if (isset($_REQUEST['download']))
{
$download_id = $_REQUEST['download'];
$db = new MysqliDatabase(ConnectionString);
$result = array();
$result = $db->query_one("SELECT TrackID, ma.ArtisteName, FeaturedArtistes,
mc.Category, TrackName
FROM `musictracks` mt
LEFT JOIN `musiccategories` mc
ON mt.CategoryID = mc.CategoryID
LEFT JOIN `musicartistes` ma
ON mt.ArtisteID = ma.ArtisteID
WHERE mt.TrackID = '$download_id';");
$filename = $result->TrackPath;
$outputfilename = $result->ArtisteName . ' ft. ' . $result->FeaturedArtistes . ' - ' . $result->TrackName . '.mp3';
header("Content-Type: audio/mpeg");
header("Content-Disposition: attachment; filename=\"" . basename($outputfilename) . "\";" );
header("Content-Transfer-Encoding: binary");
readfile("$filename");
}
And this is the download link:
<a href="<?php echo 'musicdownload.php?download='. $row->TrackID ?>" ><img src="images/download.png" alt="download" title="download" width="14" height="14" /></a>
My PHP is a bit rusty but I can think of one thing with your code, no content length header. Update your code to this and see if that works:
if (isset($_REQUEST['download'])) {
{
$download_id = $_REQUEST['download'];
// ...
$filename = $result->TrackPath;
$outputfilename = $result->ArtisteName . ' ft. ' . $result->FeaturedArtistes . ' - ' . $result->TrackName . '.mp3';
if (file_exists($filename)) {
header("Content-Type: audio/mpeg");
header("Content-Disposition: attachment; filename=\"" . basename($outputfilename) . "\";" );
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);
exit;
}
}
}
Note that we use flush(); to send the headers to the browser before we start downloading the actual file. And I also added an if (file_exists($filename)) to make sure we have a file to send. I'd recommend you put an else clause there to give you something that will show you if you don't have a file like you expect...
header("Content-Type: application/force-download");
header("Content-Type:audio/mpeg");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=".$file_name);
Please download your mp3 files using curl
Here is sample code for your reference
<?php
if(isset($_REQUEST['inputurl']) && $_REQUEST['inputurl']!="") {
$file = $_REQUEST['inputurl'];
header("Content-type: application/x-file-to-save");
header("Content-Disposition: attachment; filename=".basename($file));
readfile($file);
}
?>
<form name="from" method="post" action="">
<input name="inputurl" type="text" id="inputurl" value="" />
<input type="submit" name="Button1" value="Get File" id="Button1" />
</form>
may be it will help you.

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
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.

Categories