Size not showing while downloading files from my site [duplicate] - php

This question already has answers here:
Size not showing while downloading files from my website
(3 answers)
Closed 8 years ago.
Please see the screenshot below. When someone is downloading files from my site it, while downloading size is not showing. Thus, users cannot know how much is remaining, what is the size. Can someone help me or tell any idea to fix this? Is this a problem with the server? Or I have to put any code or something?

You should properly send the headers from your server
Better look at this :- http://php.net/manual/en/function.readfile.php
Example
<?php
$file = 'yourfile.apk';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>

Related

Viewing a file when clicked not downloaded? [duplicate]

This question already has answers here:
What is correct content-type for excel files? [duplicate]
(3 answers)
Closed 3 years ago.
When I do a Google search I find a TON of replies about the code needed to download a file when clicked and that works just fine. What I am looking for is when I click on the link it will just open up the file in the browser instead of prompting to download the file. Here is the working code to Download a file:
$file = $invoice_dir . '/test.xlsx';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
How do I just make it open up instead using my local software? It is an XLSX Office type file not plain text. I tried include() and it outputs jibberish.
You just needed to open the file in a href like this
<a href"http://www.yourdomain.com/index.php?file=file.pdf">View File</a>
and in case you want to open it in a new browser tab then
<a href"http://www.yourdomain.com/index.php?file=file.pdf" target="_blank">show file</a>
Add this code
header("Content-type: application/vnd-ms-excel");

downloading pdf files using php [duplicate]

This question already has answers here:
PHP output file on disk to browser
(6 answers)
PHP: How to make browser to download file on click
(2 answers)
Closed 5 years ago.
I have PHP files stored on my server, and their names in the mysql database, I want to download those files. What code should I write for the same? I am using PHP as coding language. Please help.
<?php
$file = 'send_me.pdf';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
Obviously, set $file to the file name.
Read more about the use of readfile here.
Download?
Literally just make a link the stored file.
file_put_contents("PDFName.pdf", fopen("http://someurl/PDFName.pdf", 'r'));
You really should show what you have done so far/researched online before asking a question!
This will download the file PDFName.pdf from the url http://someurl/PDFName.pdf and put it into the same directory as the script is in.

readfile downloads corrupt files

I am facing a problem when i am trying to download a certain file from my localhost. Everytime i download a file, either a .docx, a .pdf or a .png it is always corrupt. I have spend multiple hours on finding a solution but nothing seems to work.
This is the script im running :
$file_download="loonadministratie/{$loon_id}/{$file}";
if (file_exists($file_download)) {
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream/loonadministratie/{$loon_id}/");
header('Content-Disposition: attachment; filename='.basename($file_download));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_download));
readfile($file_download);
exit;
}
Thanks in forehand
Try
<?php
if (ob_get_level()) ob_end_clean();
?>
It may be the issue

Download file to website using php [duplicate]

This question already has answers here:
Download File to server from URL
(12 answers)
Closed 8 years ago.
I have a file with URL !
$url = "http://www.example.com/aa.txt";
and I want to download this file and save it to path on my website
this is my website ( online )
$path = "server/username/";
i want the $url file saved to $path ,,
i try this
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
when I test the code , it make the file download to my computer not to my website
you can use file_put_contents();
see manual here:http://php.net/manual/en/function.file-put-contents.php
a different implementation here:http://www.finalwebsites.com/forums/topic/php-file-download

ModX Evo: PHP readfile() in snippet?

I have a script that downloads a large file (1.3gb) using readfile().
If I create a .php page with just the script on it it works fine, but if I place the same script in a Snippet and place it on a page nothing happens.
Is ModX blocking the download some how? Any advice would be great thanks!
EDIT code:
$file = $_SERVER['DOCUMENT_ROOT']."/movie.mov";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
};
Modx does have a maximum file size setting [Maximum upload size upload_maxsize] but that is for the file manager. I doubt that is your problem.
let's see the script and error logs.
UPDATE
just tested your little snippet out [with a couple of minor changes] ~ it works fine.
$base_path = $modx->config['base_path'];
$movie = 'frankenweenie-mrwhiskers_r640s.mov';
$file = $base_path.$movie;
if (file_exists($file)) {
echo 'file exists '.filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
return true;
}else{
echo 'file does not exist';
return false;
}
using the $modx->config['base_path'] is not your problem, it worked using the server vars as well, it's just a good habit. as are the returning true/false modx expects it's snippets to return something whether true, false or $output ... also, not your problem it worked without.
Start looking at your php settings, I think possibly memory limit could be the problem. Check the php docs & see if it needs to have enough memory available to read a file that size. [even though it indicates 'size doesn't matter']
enable error logging in the script itself & check the server error logs.
It works with small files? Then look here: PHP readfile() and large downloads
Good luck!

Categories