Monday, April 30, 2012

Downloading a file forcibly using php


This is a very important and useful topic and we programmers normally need this kind of script in our websites etc. I wrote this scripts and now I am going to share you this to my blog readers.

The idea behind this is to use php header parameters to do this. Whenever you try to open some file which browser cant open (files like .doc, .docx etc ) itself, browser prompts you a download window and will ask you to download the file. This thing is done automatically by browser for some file formats. But what will happen when you open a .html, or .php file using a url? The answer to this is that browser will open or try to run these files in the browser window. But if you want to just download the files and not to open them in browser, then you have to tell php that download these files. This can be done by using some php header settings. The class that I am using for this thing is shown below.

 <?php  
 session_start();  
 $path="";  
 $filename = $path.$_GET['file'];  
 // required for IE, otherwise Content-disposition is ignored  
 if(ini_get('zlib.output_compression'))  
  ini_set('zlib.output_compression', 'Off');  
 // addition by Jorg Weske  
 $file_extension = strtolower(substr(strrchr($filename,"."),1));  
 if( $filename == "" )   
 {  
  echo "<html><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";  
  exit;  
 } elseif ( ! file_exists( $filename ) )   
 {  
  echo "<html><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";  
  exit;  
 };  
 switch( $file_extension )  
 {  
  case "pdf": $ctype="application/pdf"; break;  
  case "exe": $ctype="application/octet-stream"; break;  
  case "zip": $ctype="application/zip"; break;  
  case "doc": $ctype="application/msword"; break;  
  case "docx": $ctype="application/msword"; break;  
  case "xls": $ctype="application/vnd.ms-excel"; break;  
  case "xlsx": $ctype="application/vnd.ms-excel"; break;  
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;  
  case "pptx": $ctype="application/vnd.ms-powerpoint"; break;  
  case "gif": $ctype="image/gif"; break;  
  case "png": $ctype="image/png"; break;  
  case "jpeg":  
  case "jpg": $ctype="image/jpg"; break;  
  default: $ctype="application/force-download";  
 }  
 header("Pragma: public"); // required  
 header("Expires: 0");  
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");  
 header("Cache-Control: private",false); // required for certain browsers   
 header("Content-Type: $ctype");  
 // change, added quotes to allow spaces in filenames, by Rajkumar Singh  
 header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );  
 header("Content-Transfer-Encoding: binary");  
 header("Content-Length: ".filesize($filename));  
 readfile("$filename");  
 exit();  
 ?>  

you can download this script using this url
http://dl.dropbox.com/u/37908711/public_scripts/php_downloadfile.rar

How to resolve 1045 error when installing xamp

Some times whenever we install xamp on our system we get 1045 error in phpmyadmin. Here is the screen shot of that error,


I have found the solution of this error. Just un-comment // $cfg['Servers'][$i]['AllowNoPassword'] = TRUE; in phpmyadmin/config.inc.php file. Enjoyee....

Sunday, April 29, 2012

How to search within a defined radius using MySql and PHP

Hey guys, today my manager assigned me a task to search property listing withing a specific radius like within 10 km or 10 miles. I did it very easily because I have done this already in one of my project. But first time this task was very hard to do and I did a lot of research and goggled the internet to find out the solution. Well that is an old and long story. Today after doing my task I thought why not I post my work in the form of an article on my blog. So here is the article...

Tuesday, April 24, 2012

How to develop a "Hello World" plugin in wordpress

Hi all, hope you all are doing well and enjoying best of your healths.

Today i had been searching word-press official website for any tutorial for "how to develop a hello world plugin" and i couldn't find any good tutorial for this. Then i studied some other tutorials and asked my friends about it and thought that why not i should post this on my blog with very simple words, so that others can find a good tutorial on this. So here we go for the tutorial....

Learning wordpress hooks - simple tutorial

this coming soon......

Tuesday, April 17, 2012

How to develop a custom plugin in pyrocms


Today I am going to tell you how we can develop our own custom plug-in in pyrocms. In pyrocms whenever we edit some theme, or some view files, sometimes you will see some tags like {{ session:data name="foo" value="bar" }}. These are actually pyrocms plug-ins.

What is a plug-in?

In computing, a plug-in (or plug-in) is a set of software components that adds specific abilities to a larger software application.

In pyrocms there are two types of plug-in

  1. Standalone plug-in
  2. Modular plug-in

Standalone Plug-in:

Stand alone is a general plugin which is not necessarily dependent to other modules etc, like Google map, a simple twitter application etc. In pyrocms standalone plug-in are location in addons/plugins/.php

Modular plug-in:

Modular plug-in is actually a part of module which we already have developed. It actually adds more functionality in our existing module, that’s why it is located in the same module directory like addons/modules//plugin.php

How to develop this?

Create a file in addons/shared_addons/plugins/ directory with name example,php. And then put the following code in it and save.


1:  <?php defined('BASEPATH') OR exit('No direct script access allowed');  
2:  /**  
3:   * Example Plugin  
4:   *  
5:   * Quick plugin to demonstrate how things work  
6:   *  
7:   * @package          PyroCMS  
8:   * @author          PyroCMS Dev Team  
9:   * @copyright     Copyright (c) 2009 - 2010, PyroCMS  
10:   *  
11:   */  
12:  class Plugin_Example extends Plugin  
13:  {  
14:       /**  
15:        * Hello  
16:        *  
17:        * Usage:  
18:        * {{ example:hello }}  
19:        *  
20:        * @param     array  
21:        * @return     array  
22:        */  
23:       function hello()  
24:       {  
25:            $name = $this->attribute('name', 'World');  
26:            return 'Hello '.$name.'!';  
27:       }  
28:  }  
29:  /* End of file example.php */  

How to use this?

Using this plugin is very simple. just put {{ example:hello }} tag in you them or in any view file and save it. By refreshing your page you will see "Hello World!" as output.

Please feel free to post your comments or queries.

Monday, April 16, 2012

Making clean string for SEO friendly urls using php

SEO stands for search engine optimization, and it is very important aspect of web business and its success. In order to make a website SEO friendly, we have to take care or many SEO rules. One of which is SEO friendly urls. SEO friendly url should have meaningful keywords so that spider can find the exact content of page.

The function to make clean url is

 private function clean_url($text)  
  {  
       $text=strtolower($text);  
       $code_entities_match = array(' ','--','&quot;','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=','"');  
       $code_entities_replace = array('-','-','','','','','','','','','','','','','','','','','','','','','','','','');  
       $text = str_replace($code_entities_match, $code_entities_replace, $text);  
       return $text;  
 }  

took help from http://www.9lessons.info/2011/04/seo-friendly-urls-with-php.html

How to find first date and last date of month using php

Hi guys, i am going to share and very simple but very important function with you. In this post i am gonna show you how to find first date and last date of current month using php. Here are the two functions,

 <?php  
 function firstOfMonth()  
 {  
      return date("m/d/Y", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));  
 }  
 function lastOfMonth()  
 {  
      return date("m/d/Y", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));  
 }  
 ?>  

PHP Date subtraction in hours, minutes, weeks formate

Some times we need to subtract two dates and we need the output in the form of months, weeks, hours and minutes etc. For doing to I wrote a function in php and i would like to share this with you guys...

 <?php  
 function create_time_range($start, $end, $by='30 mins')  
 {  
   $start_time = strtotime($start);  
   $end_time = strtotime($end);  
   $times = array();  
   for ( ;$start_time < $end_time; )  
   {  
     $times[] = $start_time;  
     $start_time = strtotime('+'.$by, $start_time);  
   }  
   $times[] = $start_time;  
   return $times;  
 }  
 // create array of time ranges  
 $times = create_time_range('00:00', '23:00', '30 mins');  
 // more examples  
 // $times = create_time_range('9:30am', '5:30pm', '30 mins');  
 // $times = create_time_range('9:30am', '5:30pm', '1 mins');  
 // $times = create_time_range('9:30am', '5:30pm', '30 secs');  
 // and so on  
 // format the unix timestamps  
 foreach ($times as $key => $time) {  
   $times[$key] = date('G:i', $time);  
 }  
 ?>  


Friday, April 13, 2012

Thursday, April 12, 2012

How to run two skype instances in windows

Hi all, today i am gonna show you a very interesting thing. I was doing an experiment of running two skype on one computer at a time because i have multiple skype accounts and i want to login to them at a time. My friend told me this trick and this made my life so easy. I want to share with my blog readers as well.

Pyrocms - Creating hello world module in pyrocms | Knowledge Base

Pyrocms - Creating hello world module in pyrocms | Knowledge Base

Monday, April 9, 2012

Pyrocms - Creating hello world module in pyrocms

Custom module development :-

Hi all, today i am gonna show you how you can create a simple "hello world" module in pyrocms. You can do so by following some simple steps mentioned in this article.

Pyrocms - Step by step tutorial to create widget in pyrocms

Hi all, today i am going to tell you how to create a pyrocms widget in some easy steps. Widgets are a small program to show some text or some login form etc on your website. The widget we are going to develop in this tutorial, also outputs some simple text on sidebar. So here are the steps,