How to download .sql.bz2 file using php - php

I'm trying to download .sql.bz2 file using php. But i am unable to do it.
My code is downloading file but file not opening. I want file location to be hidden from user.
I am using following code:
$folderroot = $_SERVER['DOCUMENT_ROOT'];
$fileurl = $folderroot."/dbname.sql.bz2";
$downloadfilename = generaterandomcharacters(10).".sql.bz2";
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename=test.sql.bz2');
header('Pragma: no-cache');
readfile($fileurl);

I just tried your code and it worked for me.
So I'd say that the value your giving to $fileurl is probably incorrect.
Try this :
$folderroot = $_SERVER['DOCUMENT_ROOT'];
$fileurl = $folderroot."/dbname.sql.bz2";
if ( file_exists($fileurl) ) {
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename=test.sql.bz2');
header('Pragma: no-cache');
readfile($fileurl);
}
else
{
echo 'cannot find file : ' . $fileurl;
}

Related

Download files pdf/jpg/png invalid in PHP

I'm trying to download pdf/jpg/jpeg/png using PHP. Here is my code
$result= mysqli_query("SELECT * FROM upload WHERE upload_id='$id'");
$row = mysqli_fetch_array($result);
//get file extension
$filename = $row['upload_name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if($ext=='jpg'||$ext=='jpeg'||$ext=='png' ||$ext=='pdf'){
echo "Download";
}
download.php
$name= $_GET['id'];
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($name) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($name));
ob_clean();
flush();
readfile("uploads/".$name); //showing the path to the server where the file is to be download
exit;
When I tried the above code, it can be download however, when I tried opening the pdf/jpg/png it says invalid. Is there anything wrong with my code? This is my first time using the download code. So I really dont know what is the problem.
And for this path as stated in the code below,
readfile("uploads/".$name);
How do I write it if my pdf/jpg/png files are saved here in my computer C:\Apache2.2\htdocs\epals\uploads
Please help thanks

PHP forced download going to webpage

I have an issue with the force download of a file.
I'm using PHPExcel to create a xls file based on information extracted from our database. This all works fine and the Excel file works as required.
I then attempt to force download of created file using the following function. However it downloads webpage rather than as a file.
Currently developing on Win XP SP3, Notepad++, XAMPP (Apache 2.4.3, PHP 5.4.7).
**function following
public function downloadfile($file){
if(file_exists($file) && is_file($file)){
//ob_start();
echo $file;
echo "in file exists";
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);
//ob_end_flush();
}else
echo "file or location does not exist <br>";
echo $file;
}
Any help would be appreciated.
Thanks in advance
If its a excel file then just redirect to that url and by default it will ask for file download.
header("Location: http://site.com/path/to/file/filename.xlsx");
/* Make sure that code below does not get executed when we redirect. */
exit;
If in javascript give
location.href = 'http://site.com/path/to/file/filename.xlsx';
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: inline; filename=\"" . $fileName . ".xls\"");
header("Pragma: no-cache");
header("Expires: 0");
readfile($filePath);
try this

Download File in Yii

I am trying to write a script in Yii for downloading files from the server.
The files are located in webroot of the Yii project,
but I got every time file not exist error, could anyone see where is wrong:
public function actionDownload($id) {
$audio = Audio::model()->findByPk($id);
$file = Yii::getPathOfAlias('webroot') . $audio->path;
if (file_exists($file)) {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $audio->path);
header('Content-Length: ' . filesize($audio->path));
$audio->downloaded = $audio->downloaded + 1;
$audio->save();
}else{
echo "file not exist: ".$file;
}
exit;
}
error I got is:
file not exist: /var/www/vhosts/ikhwanbiz.org/httpdocs/userfiles/reklames/media/deneme/sen%20dep%20olmisem.mp3
Thanks
Regards
Bili
Bili, this works well for me and seem to be fine on most browsers.
$filename = 'your_file_name.csv';
header('Content-Disposition: attachment; charset=UTF-8; filename="'.$filename.'"');
$utf8_content = mb_convert_encoding($content, "SJIS", "UTF-8");
echo $utf8_content;
Yii::app()->end();
return;
Hope it helps, good luck!
It looks like the filename portion $audio->path is URL-encoded, while the name of the actual file on the server is not. You should fix it at the source (no idea where that path is set from), but in the meantime an easy workaround would be to write
$file = Yii::getPathOfAlias('webroot') . urldecode($audio->path);
This is more of a php question than a yii one.
for eg,
<?php
header("Content-disposition: attachment; filename=huge_document.pdf");
header("Content-type: application/pdf");
readfile("huge_document.pdf");
?>
source: http://webdesign.about.com/od/php/ht/force_download.htm

File download from server using mysql and PHP

I have created a PHP page that allows users to download a file when they click the this link:
Download File
I have also created the download page that the link directs to:
<?php
if(isset($_GET['file'])) {
$fileID = $_GET['pubid'];
$filename= ($_GET['file']);
$path = "admin/pubfiles/";
$fullPath = $path . $filename;
mysql_select_db($database_connDioceseofife, $connDioceseofife);
$sql = "SELECT file FROM publications WHERE pubID = $fileID";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if($filename == NULL) {
die('No file exists or the name is invalid!');
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
readfile($fullPath);
}
?>
Now my problem is, when the download popup window come up, it reads that the file is of 0 bytes. And when downloaded, it can't open. i get the message that it is not a supported file type or its been damaged or corrupted.
Please any help would be much appreciated.
You're not doing anything with the query result in $row.
Use $row['file'] to get the actual file itself.
Thank you all for assisting me. I have been able to solve the problem after further reading. I have updated the initial script i wrote. what is above is now the working script.
What i did was to include $fullpath = $path . $filename then changed the header("Content-Disposition: attachment; filename=\"$filename\""); and then the readfile function from readfile($path) to readfile($fullpath).
Thanks again #nlsbshtr and everybody else for your help.

Problem naming the download file

I have some php code which is suppose to download a zip file...this code is called from a file called download_file.php (very original, i know)
The relevant function is as follows :
function download_clientfile() {
if ($this->download_file === FALSE) {
$this->log->log(sprintf("The download file was not generated."), PEAR_LOG_CRIT);
return;
}
$fsize = filesize($this->download_file);
$path_parts = pathinfo($this->download_file);
$ext = strtolower($path_parts["extension"]);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; file='.$path_parts['basename']);
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($this->download_file));
ob_clean();
flush();
readfile($this->download_file);
exit;
}
Problem : The file downloaded is the zip file that is expected, however its downloaded with the name 'download_file.php'
Any suggestions on how I can get it to download by the name of the zip file.
Thanks
Use filename instead of file:
header('Content-Disposition: attachment; filename='.$path_parts['basename']);
Also, you may encounter browsers which use the extension anyway. In that case, you can call the script like this:
download_file.php?file=myfile.zip
or:
download_file.php/myfile.zip
This way, the URL ends in .zip, which tricks the browser into thinking it is downloading a ZIP file.
Try changing file= to filename=
header('Content-Disposition: attachment; filename='.$path_parts['basename']);
use
header("Content-Disposition: attachment; filename=\"$filename\"");

Categories