function download($data = false){
$file_name = '2013-07-01-093847test';
$extension = 'txt';
$filename = $file_name.".".$extension;
$upload_folder='path\to\folder\';
$uploadDir = WWW_ROOT.$upload_folder;
$file=$uploadDir.'\\'.$file_name;
$new_file=$uploadDir.'\\'.$filename;
copy($file, $new_file);
$ext = end(explode('.',$new_file));
if($ext && array_key_exists($ext,$file_types)){
if(file_exists($new_file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/stream');
header('Content-Disposition: attachment; filename='.basename($new_file));
header('Content-Transfer-Encoding: binary');
header('Expires: 78000');
header('Cache-Control: private');
header('Pragma: public');
header('Content-Length: ' . filesize($new_file));
#readfile($new_file);
unlink($new_file);
exit;
}
}
else {
die('The provided file path is not valid.');
}
}
While I am trying to download a file using the above code from an extjs interface, the file content gets displayed at the firebug console part and not getting download to the client machine?
Related
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;
}
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();
}
}
}
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;
}
I am trying to download image file in CI but getting error when I open download image file. Need help :(
$file_name = $_GET['file_name'];
$file_path = "./ups_printimages/".$file_name;
if(file_exists($file_path))
{
$this->load->helper('download');
$data = file_get_contents($file_path); // Read the file's contents
$name = 'ups_label.png';
force_download($name, $data);
}
else
{
echo "file does not exist";
}
GOD. Found out the solution :)
if(!file)
{
File doesn't exist, output error
die('file not found');
}
else
{
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, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile($file);
exit;
}
Use header to force download. Use the code below
$file_name = $_GET['file_name'];
$file_path = "./ups_printimages/".$file_name;
if(file_exists($file_path))
{
header('Content-Type: image/jpeg');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_name) . "\"");
readfile($file_path);
}
else
{
echo "file does not exist";
}
Hope this helps yoiu
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