I am facing with strange problem while exporting my csv file on safari it is just displaying on browser instead of downloading .While same code is working with Firefox and Crome.I have searched but nothing is working for me. Please help. Here is my code-
<?php
ob_clean();
ob_start();
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
function exportData() {
$fp = fopen('php://output', 'w');
fputcsv($fp, array('Market ID', 'Market Name', 'Suburb', 'State', 'Start Time', 'End Time', 'Status',"~"));
include "database.php";
$dbquery = #$_POST['query'];
$queryAllUser = $dbquery;
$resultAllUser = mysql_query($queryAllUser);
$countAllUser = mysql_num_rows($resultAllUser);
if($countAllUser > 0)
{
while($rowMarketId= mysql_fetch_assoc($resultAllUser))
{
$marketId = $rowMarketId['mrkt_id'];
$isCancel = $rowMarketId['is_cancel'];
$openning_tim = $rowMarketId['openning_time'];
$closing_tim = $rowMarketId['closing_time'];
$suburb = $rowMarketId['suburb'];
$name = $rowMarketId['name'];
$state = $rowMarketId['state'];
if($isCancel == 0)
{
$status_type = "Open";
}
else
{
$status_type = "Close";
}
$val = array();
$val[] = $marketId;
$val[] = $name;
$val[] = $suburb;
$val[] = $state;
$val[] = $openning_tim;
$val[] = $closing_tim;
$val[] = $status_type;
$val[] = "~";
fputcsv($fp, $val);
}
}
}
exportData();
?>
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' .urlencode(basename($filename)));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
Try With this Headers........ This should Work..
Related
I am using following code to download csv file.
static function download(){
$wooe_download = filter_input(INPUT_GET, 'woooe_download', FILTER_DEFAULT);
$wooe_filename = filter_input(INPUT_GET, 'filename', FILTER_DEFAULT);
if( !empty($wooe_filename) && !empty($wooe_download) && file_exists(path_join( self::upload_dir(), $wooe_filename.'.csv'))
&& wp_verify_nonce($wooe_download, 'woooe_download')
)
{
$charset = get_option('blog_charset');
$csv_file = path_join( self::upload_dir(), $wooe_filename.'.csv');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=". self::filename());
header("Expires: 0");
header('Cache-Control: must-revalidate');
header('Content-Encoding: '. $charset);
header('Pragma: public');
//header('Content-type: text/csv; charset='. $charset);
header('Content-Length: ' . filesize($csv_file));
//header("Pragma: public");
readfile($csv_file);
//unlink($csv_file);
exit;
}
}
File is getting downloaded twice in browser. I am not sure how to restrict it to only once?
I want to get information and download it through a file. But it just open a new window and show the information in it.
$filename = $this->input->cookie('merchant_id');
$timestamp = date("YmdHis",time());
$filename .= $timestamp.'.'.$fileType;
$title = mb_convert_encoding($title,'gbk','utf-8');
header('Content-type: application/octet-stream');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Content-Disposition: attachment; filename=' . $filename);
echo $title;
$ret = $this->download_model->QueryList($Req, $Resp);
$content = $this->GetDownLoadContentNew($ret['bill_list'], $fileType);
echo $content;
The GetDownLoadContentNew function just format the content.
So, why it cannot download?
I have created the text file and downloading when I execute the code. But I need to store this downloaded file in a specified folder inside the server. I have tried in many ways, but I didn't get any solution. I have mentioned my code below, Can anyone guide me?
$saving_name = $i.''.$machineid.'.'.$count_pad;
foreach($data['SalesDetails'] as $d)
{
$val = $d->inv_total - $d->inv_discount;
$explodedval = explode(".",$val);
$totalval = str_pad($explodedval['0'], 8, "0", STR_PAD_LEFT);
$totalvals = str_pad($explodedval['1'], 2, "0", STR_PAD_RIGHT);
$result = $totalval.'.'.$totalvals;
$dates = str_replace("-","",$d->date);
$out .= $i.''.$machineid.''.$dates.''.$result.' '."\r\n";
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$saving_name.'.txt');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $out;
exit;
Here is the path need to move the downloading file, /var/www/html/art/assets/uploads/edi/sample.txt
Thank you for your support. Finally I got solution for this which is shown below:
$saving_name = $i.''.$machineid.'.'.$count_pad;
foreach($data['SalesDetails'] as $d)
{
$val = $d->inv_total - $d->inv_discount;
$explodedval = explode(".",$val);
$totalval = str_pad($explodedval['0'], 8, "0", STR_PAD_LEFT);
$totalvals = str_pad($explodedval['1'], 2, "0", STR_PAD_RIGHT);
$result = $totalval.'.'.$totalvals;
$dates = str_replace("-","",$d->date);
$out .= $i.''.$machineid.''.$dates.''.$result.' '."\r\n";
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$saving_name.'.txt');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $out;
$backup_file = '/var/www/html/artbak/assets/uploads/edi/'.$saving_name.'.txt';
//save file
$handle = fopen($backup_file,'w+');
fwrite($handle,$out);
fclose($handle);
exit;
Now the file stored in the specified folder as well download also.
I try to do so that I can download files but I have a problem and don't know how to fix it can you help me find the error?
Nothing happens. And help me to get this to work. I don't know how to fix this so need all the help I can get.
<?php
require("configuration.php");
require("include.php");
require_once('./libs/phpseclib/SFTP.php');
require_once("./libs/phpseclib/Crypt/AES.php");
if(!isset($_SESSION['clientid']))
{
//DON'T KNOW HOW THE REQEUSTOR IS!!
die();
}
$clientid = $_SESSION['clientid'];
$serverid = '';
$extendedPath = '';
$action = '';
if(!isset($_GET['serverid']) or !isset($_GET['path']) or !isset($_GET['action']))
{
die();
}
$serverid = $_GET['serverid'];
$extendedPath = $_GET['path'];
$action = $_GET['action'];
$boxDetailsSQL = sprintf("SELECT box.boxid, box.ip, box.login, box.password, box.sshport, srv.path
FROM %sbox box
JOIN %sserver srv ON box.boxid = srv.boxid
JOIN %sgroupMember grpm ON (grpm.groupids LIKE CONCAT(srv.groupid, ';%%')
OR grpm.groupids LIKE CONCAT('%%;', srv.groupid, ';%%'))
WHERE srv.serverid = %d
AND grpm.clientid = %d;", DBPREFIX, DBPREFIX, DBPREFIX, $serverid, $clientid);
$boxDetails = mysql_query($boxDetailsSQL);
$rowsBoxes = mysql_fetch_assoc($boxDetails);
$aes = new Crypt_AES();
$aes->setKeyLength(256);
$aes->setKey(CRYPT_KEY);
$sftp= new Net_SFTP($rowsBoxes['ip'], $rowsBoxes['sshport']);
if(!$sftp->login($rowsBoxes['login'], $aes->decrypt($rowsBoxes['password'])))
{
echo 'Failed to connect';
die();
}
//ACTION SELECTOR
if($action == 'list')
{
getlist($rowsBoxes, $extendedPath, $sftp);
}
if($action == 'fileUpload')
{
fileUpload($rowsBoxes, $extendedPath, $sftp);
}
if($action == 'download')
{
delete($rowsBoxes, $extendedPath, $sftp);
}
//ACTION FUNCTIONS
function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile $sftp->put($remoteFile);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downloadfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($downloadfile));
readfile($downloadfile);
}
This code I need help to fix.
function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile $sftp->put($remoteFile);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downloadfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($downloadfile));
readfile($downloadfile);
}
You're calling getList, fileUpload or delete, depending on the value of $_GET['action'] but you're not calling download at all. You've defined that function but you aren't calling it in your code snippet. And in your download function...
$downloadfile $sftp->put($remoteFile);
You should probably be doing this instead:
$downloadfile = $sftp->get($remoteFile);
Also, instead of doing filesize($downloadfile) do strlen($downloadfile) and instead of readfile($downloadfile) do echo $downloadfile;. If you want to save to a local file do $sftp->get($remoteFile, $downloadfile);
Updated code:
function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile = $sftp->get($remoteFile);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downloadfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($downloadfile));
echo $downloadfile;
}
If you want to do something like display an image do this:
function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile = $sftp->get($remoteFile);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension ) {
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default:
}
header('Content-type: ' . $ctype);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo $downloadfile;
}
need to generate a txt file with the following output html code
<?php
function video() {
$video = 'BYN-DEM7Mzw';
echo '<script type="text/javascript">
function youtubeFeedCallback( data ){
document.writeln( data.entry[ "media$group" ][ "media$description" ].$t.replace( /\n/g, "<br/>" ) + "<br/>" ); }
</script>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/'.$video.'?v=2&alt=json-in-script&callback=youtubeFeedCallback"></script>';
}
ob_start();
video();
$output = ob_get_clean();
$desc = $output;
$video = 'BYN-DEM7Mzw';
$arq = $video.".txt";
$f = fopen($arq, "w+");
fclose;
$f = fopen($arq, "a+");
$content = "";
if(filesize($arq) > 0)
$content = fread($f, filesize($arq));
fwrite($f, $desc);
fclose($f);
?>
the problem that the file is saving up my script and not the output html
I think you should be using the right headers and the correct mime in it.
For example
header('Content-Description: File Transfer');
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename='.$file);
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($file));
if ($file != '')
{
echo file_get_contents($file);
}