how do i let user download file from a server - php

I am using mssql server and CI in which it auto backups the db file into certain folder now I need to let the user download the file from the server to her local machine.
$filename = basename($_GET['file']);
// Specify file path.
$path = 'backups/hello.txt';
$download_file = $path.$filename;
if(!empty($filename)){
// Check file is exists on given path.
if(file_exists($download_file))
{
header('Content-Disposition: attachment; filename=' . $filename);
readfile($download_file);
exit;
}
else
{
echo 'File does not exists on given path';
}
}
}
I have tried this one but it says file is unknown.

As per php document, can you try like this
download.php
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');//change your extension of your files
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;
}

Related

Can't play video file with more than 700mb that downloaded using XAMPP

I can't download files that already uploaded in my website, for example :
I already uploaded a video file with 800mb file size and it is okay, the file is save to my directory in the File folder, then I want to download that file again, but as soon as I download the file, I will always got 1kb file not the exact size of the file, and when I play it, nothing happen
this is my code:
<?php
include 'db.php';
if(isset($_REQUEST['name']))
{
$var =$_REQUEST['name'];
$dir = "../files/";
$file = $dir . $var;
if(file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: video');
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);
mysqli_close($conn);
exit();
}
else{
echo "File not found";
}
}
?>
Yeah, I had the same problem too. After searching alot on the internet, I found out that the problem is related to output buffering. The following code solved my problem.
<?php
$file = $_GET['file'];
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_end_clean(); //adding this line solves my problem
readfile($file);
exit;
?>
The code ob_end_clean() basically runs grabs everything in the buffer, then erases the buffer, and turns off output buffering.

Downloading file from another website in PHP creates the file but doesn't download any bytes

I'm trying to download this file from DropBox, i tried the same code with a file on the same website that running the PHP script and it did work but now when i add a full ULR, it doesn't.
The problem is that the QR-Kodite.rar file is created in my Windows downloads folder but its size is 0 and Chrome says that the download has finished.
This is my code :
if(isset($_GET['App'])){
if(basename($_GET['App']) == $_GET['App']){
$path = 'https://dl.dropboxusercontent.com/s/pdg8bnpmbgvcsmd/QR-Kodite.rar';
$size = filesize($path);
header('Content-Type: application/octet-stream');
header('Content-Length: ' . $size);
header('Content-Disposition: attachment; filename=' . $path);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
$file = # fopen($path, 'rb');
if($file){
fpassthru($file);
$_SESSION["ITA"] = "No";
exit();
}
}
}

PHP downloading files has been corrupted(While downloading from ftp server)

I have problem in downloading file(php)from a particular folder.
when i download and open the file it says your file is corrupted.
when i check the size of the uploaded file and downloaded file it is same , but for zip file size it differs.
No Files are opening.
can any one say where i am wrong???
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
$filename = $_GET['file'];
}
else
{
$filename = NULL;
}
$err = 'Sorry, the file you are requesting is unavailable.';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo $err;
}
else
{
// define the path to your download folder plus assign the file name
$path = '/public_html/wp-content/uploads/'.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
$size = filesize($path);
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment;filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error messages if the file canĀ“t be opened
$file = # fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
exit;
}
inserting into table:
echo "<tr><td><a href='?file=" . $row["FileupName"]. "'>".$row["FileupName"]."</td></tr>";
I am happy that the file is getting downloaded but it not getting opened.
.txt file is getting opened.
Had checked with header also.
i have tried putting:
ob_clean();
flush();
readfile($file);
if (file_exists($path)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($path));
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($path));
ob_clean();
flush();
readfile($path);
exit;
}

Cannot download file using fpassthru?

I have scripted a download file on PHP. I use fpassthru but I cannot download any file. It just print binary code on my page. When I tried uploading it on host and it works fine.
Can anyone explain why my PHP file is not working on local?
Note: I am using XAMPP on Mac.
Here is my code:
$filename = $data['PathFile'];
$directory = '../upload/'.$filename;
if (file_exists($directory) && is_readable($directory)) {
$size = filesize($directory);
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
$file = # fopen($directory, 'rb');
if ($file) {
fpassthru($file);exit;
}
Please Check your Directory or path. Giving an example of another code for download please try this.
$file = 'Pareshaan.mp4';
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;
}

Downloading file from localhost site in PHP

When i am download a file with extention like mp3,flv etc it will now download it start buffering And when I remove extention from the file it downloads ...
can you please tell whats the problem behind that ...
file to download is => theangelfoundation-12-5-2012-09-17-27-somebody.mp3
Thanks in advance ..
$file_types=array();
$file_types['mp3'] ='audio/mpeg';
$file_types['mpeg'] ='video/mpeg';
$file_types['mpg'] ='video/mpeg';
$file_types['pdf'] ='application/pdf';
$file_types['pps'] ='application/vnd.ms-powerpoint';
$file_types['ppt'] ='application/vnd.ms-powerpoint';
$file_types['ps'] ='application/postscript';
$file = 'you.mp3';
download($file,$file_types);
function download($file_name,$file_types){
$file = $file_name;
$ext = end(explode('.',$file_name));
if($ext && array_key_exists($ext,$file_types)){
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;
}
}
else {
die("this is not a downloadable file");
}
}
?>
download ok.txt

Categories