Download File Function in Cakephp

public function download_discussion_file($file_name)
{

$file_path = ROOT . DS . ‘app’ . DS . ‘webroot’ . DS . ‘img/uploads’ . DS;
$file_path = $file_path.’/’.$file_name;
//output_file($file_path, ”.$file_name.”, ‘text/plain’);

$file = $file_path;
$name = $file_name;
$mime_type = ”;
if(!is_readable($file)) die(‘File not found or inaccessible!’);

$size = filesize($file);
$name = rawurldecode($name);
$known_mime_types=array(
“pdf” => “application/pdf”,
“txt” => “text/plain”,
“html” => “text/html”,
“htm” => “text/html”,
“exe” => “application/octet-stream”,
“zip” => “application/zip”,
“doc” => “application/msword”,
“xls” => “application/vnd.ms-excel”,
“ppt” => “application/vnd.ms-powerpoint”,
“gif” => “image/gif”,
“png” => “image/png”,
“jpeg”=> “image/jpg”,
“jpg” => “image/jpg”,
“php” => “text/plain”
);
if($mime_type==”){
$file_extension = strtolower(substr(strrchr($file,”.”),1));
if(array_key_exists($file_extension, $known_mime_types)){
$mime_type=$known_mime_types[$file_extension];
} else {
$mime_type=”application/force-download”;
};
};

@ob_end_clean();

if(ini_get(‘zlib.output_compression’))
ini_set(‘zlib.output_compression’, ‘Off’);
header(‘Content-Type: ‘ . $mime_type);
header(‘Content-Disposition: attachment; filename=”‘.$name.'”‘);
header(“Content-Transfer-Encoding: binary”);
header(‘Accept-Ranges: bytes’);
header(“Cache-control: private”);
header(‘Pragma: private’);
header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”);
if(isset($_SERVER[‘HTTP_RANGE’]))
{
list($a, $range) = explode(“=”,$_SERVER[‘HTTP_RANGE’],2);
list($range) = explode(“,”,$range,2);
list($range, $range_end) = explode(“-“, $range);
$range=intval($range);
if(!$range_end) {
$range_end=$size-1;
} else {
$range_end=intval($range_end);
}
$new_length = $range_end-$range+1;
header(“HTTP/1.1 206 Partial Content”);
header(“Content-Length: $new_length”);
header(“Content-Range: bytes $range-$range_end/$size”);
} else {
$new_length=$size;
header(“Content-Length: “.$size);
}
$chunksize = 1*(1024*1024);

$bytes_send = 0;
if ($file = fopen($file, ‘r’))
{
if(isset($_SERVER[‘HTTP_RANGE’]))
fseek($file, $range);

while(!feof($file) &&
(!connection_aborted()) &&
($bytes_send<$new_length)
)
{
$buffer = fread($file, $chunksize);
print($buffer);
flush();
$bytes_send += strlen($buffer);
}
fclose($file);
} else

die(‘Error – can not open file.’);
die();

}

Progress bar using Ajax, Jquery and Bootstrap

HTML Code:

<div class=”progress” style=”width:90%; margin:auto; position:fixed; bottom:10px; left:5%;”>
<div class=”progress-bar bar-contest” role=”progressbar” aria-valuenow=”70″ aria-valuemin=”0″ aria-valuemax=”100″ style=””>
</div>
</div>

Ajax Code:

$.ajax({
xhr: function()
{
var xhr = new window.XMLHttpRequest();
//Upload progress
xhr.upload.addEventListener(“progress”, function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with upload progress
var myString = percentComplete;
var sNumber = percentComplete.toString();
var final = sNumber.split(‘.’);
if(final[0] != 1){
var final2 = final[1].substring(0,2)+’%’;
} else {
var final2 = 100+’%’;
$(‘.bar-contest’).addClass(‘progress-bar-success’);
}
$(‘.bar-contest’).css(‘width’, final2);
$(‘.bar-contest’).html(final2);
console.log(final2);
}
}, false);
//Download progress
xhr.addEventListener(“progress”, function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with download progress
console.log(percentComplete);
}
}, false);
return xhr;
},

url: “add_contest_data.php”,
type: “POST”,
data: new FormData(this),
dataType: “json”,
contentType: false,
cache: false,
processData:false,
success: function(data) {
}

});

Multiple Fields Explode in PHP foreach Loop

if($sh[‘portfolio_files’]!=””){
$port_files = array_reverse(explode(‘||’, $sh[‘portfolio_files’]));
$port_name = array_reverse(explode(‘||’, $sh[‘portfolio_text’]));
$port_covers = array_reverse(explode(‘||’, $sh[‘portfolio_covers’]));
foreach($port_files as $index => $port_pics){
echo “echo all content with explode data.”;
}
}

Create Shortcode in PHP WORDPRESS

add_shortcode( ‘FEATUREDPOST’, ‘visitor_check_shortcode’ );
function visitor_check_shortcode($atts) {
echo ‘<h3 class=”widget-title”>Premium Biz</h3><div class=”featured-classified-ads”><ul>’;
$cartlimit = get_option(‘my-setting1’);
$limit = $cartlimit . “00”;
$sql = mysql_query(“select * from bbs_awpcp_ads where ad_item_price > ‘$limit’ and disabled = ‘0’ order by ad_id DESC LIMIT 8”);
while($ad = mysql_fetch_array($sql)){
$id = $ad[‘ad_id’];
$ss = mysql_query(“select * from bbs_awpcp_adphotos where ad_id = $id”);
$ks = mysql_fetch_array($ss);
$featuredimage = $ks[‘image_name’];
$src = ‘http://businessforbuysale.com/wp-content/uploads/awpcp/&#8217;.$featuredimage;
$noimg = “no_image.jpg”;
if (@getimagesize($src)) {
$featuredimage = $featuredimage;
}
else
{
$featuredimage = $noimg;
}
echo ‘<a href=”http://businessforbuysale.com/business/show-listing/&#8217;. $ad[‘ad_id’] .’/’. $ad[‘ad_title’].'”><li><img src=”http://businessforbuysale.com/wp-content/uploads/awpcp/&#8217; . $featuredimage .'” /><br /><p>’;
echo custom_echo($ad[‘ad_title’]);
echo “</p></li></a>”;
}
echo “</ul></div>”;

}

SHORTCODE : [FEATUREDPOST] to use

Export Mysql to Excel in PHP

<?php
/*
* Export Mysql Data in excel or CSV format using PHP
* Downloaded from http://DevZone.co.in
*/

// Connect to database server and select
$con=mysqli_connect(‘Localhost’,’novaire_123′,’Novaire01′,’admin_hvdec’);

if (mysqli_connect_errno()) {
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}

// retrive data which you want to export
$query = “SELECT * FROM `hvdc_users`”;
$header = ”;
$data =”;

$export = mysqli_query($con,$query ) or die(mysqli_error($con));

// extract the field names for header

while ($fieldinfo=mysqli_fetch_field($export))
{
$header .= $fieldinfo->name.”\t”;
}

// export data
while( $row = mysqli_fetch_row( $export ) )
{
$line = ”;
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == “” ) )
{
$value = “\t”;
}
else
{
$value = str_replace( ‘”‘ , ‘””‘ , $value );
$value = ‘”‘ . $value . ‘”‘ . “\t”;
}
$line .= $value;
}
$data .= trim( $line ) . “\n”;
}
$data = str_replace( “\r” , “” , $data );

if ( $data == “” )
{
$data = “\nNo Record(s) Found!\n”;
}

// allow exported file to download forcefully
header(“Content-type: application/octet-stream”);
header(“Content-Disposition: attachment; filename=userdata.xls”);
header(“Pragma: no-cache”);
header(“Expires: 0”);
print “$header\n$data”;

?>

Website Search Query in PHP Mysql

$d = “”;
$search = $_POST[‘search’];
if(!$search == “”){
$d .= “`Property` like ‘%” .$search. “%’ and “;
}
else
{
$d .= “`Property` like ‘%%’ and “;
}

$rate = $_POST[‘rate’];
if(!$rate == “”){
$d .= “`Prize` =  ” . $rate . ” and “;
}

$area = $_POST[‘area’];
if(!$area == “”){
$d .= “`Total_sq` =  ” . $area . ” and “;
}

$county = $_POST[‘county’];
if(!$county == “”){
$d .= “`County` = ‘” . $county . “‘ and “;
}

$type = $_POST[‘type’];
if(!$type == “”){
$d .= “`Type` = ” . $type;
}
else
{
$d .= “`Type` like ‘%%'”;
}

$sql = mysql_quer(“SELECT COUNT(*) AS id FROM `hvdc_property` where ” . $d);

Add Images in Upload Folder and Database

$filename=””;
if(getimagesize($_FILES[“file1”][“tmp_name”])){
$temp_file_extn = strtolower(end(explode(‘.’,$_FILES[“file1”][“name”])));
$filename = mt_rand(0,9999).’file1′.time().’.’.$temp_file_extn;
move_uploaded_file($_FILES[“file1”][“tmp_name”],’storyupload/’.$filename);
}