Reputation: 355
I am trying to make the .txt, .xml,.sql files be downloadable in the same page just like .zip,.docx files which are pop up and ask "do you want to download this file?". I have researched and found that the windows default option for these files are open in new browser but I just want to be download in the same page. I have attached the image just like that I need to pop up when clicking those .txt,.xml,.sql files. How can this be done? Anyone have idea for this.
Upvotes: 1
Views: 2818
Reputation: 1005
You can use this and it will work like a charm .
<?php
$filename = $_GET["file"];
$contenttype = "application/force-download";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
if($_GET['path'] == 'new_uploads')
readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/new_uploads/".$filename);
else
readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/assessment_uploads/".$filename);
exit();
?>
Upvotes: 2
Reputation: 355
I have corrected by my own. Just putting the code below will successfully pop up any files to ask whether to download or not. This is my new code.
The download.php contains the code below:
<?php
$filename = $_GET["file"];
$contenttype = "application/force-download";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
if($_GET['path'] == 'new_uploads')
readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/new_uploads/".$filename);
else
readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/assessment_uploads/".$filename);
exit();
?>
And the file.php contains this code:
<img src="<?php echo base_url();?>assets/plupload/assessment_uploads/<?php echo $files->file_name?>" title="DOWNLOAD IMAGE" height="100" width="100" onClick="window.location.href='<?php echo base_url();?>logos/download?file=<?php echo $files->file_name?>&path=assessment_uploads'" class="download"/>
This will correctly sort out the current issue.
Upvotes: 2
Reputation: 11
You should pass the headers in the code where file download is requested.
$task = isset($_REQUEST['task']) ? $_REQUEST['task'] : '';
if ($task == 'download'){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($_REQUEST['file_name']));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($_REQUEST['file_name']));
ob_clean();
flush();
readfile($_REQUEST['file_name']);
exit;}
Upvotes: 1