PHP: Code to download a file

Hello friends,
In last week I had a requirement to give links of files to user. And if user will click on that file’s link then that file will be downloaded to his/her PC.
To satisfy the above requirement I had searched the web and found many solutions for that. But our site is in PHP language, so all the solutions were not valid for me. Where at last I found a good PHP code to download a file and here I want to share that code with you people.
Now to implement download file facility using PHP code follow the under given steps.
Step 1: Copy and paste under given code in your PHP file.

$filepath = 'Give full path of your file';
$filename = 'Give file name'.
ob_end_clean();
header('MIME-Version: 1.0');
header('Pragma: ');
header('Cache-Control: public');
header('Content-length: '.filesize($filepath));
header('Content-type: '.mime_content_type($filepath));
header('Content-transfer-encoding: 8bit');
header('Content-disposition: attachment; filename="'.$filename.'"');
$handle = fopen($filepath, 'rb');

if ($handle)
{
	while (!feof($handle)) {
		print fread($handle, 8192);
	}
	fclose($handle);
}
flush();

Step 2: On first line replace the ‘Give full path of your file’ with full path of your file which you want to download.
Step 3: On second line replace the ‘Give file name’ with file name of your file which you want to download.
Step 4: Save the file and execute it.
Hope this will help you.
Thanks.

How to apply a complex theme over Magento?

Hello Friends,
Applying a theme over Magento is not so easy as installing a theme in Magento. Applying a theme requires few steps to be followed one after one carefully to apply the theme.
Sometimes the theme seems too much complex because of it’s functionalities to be applied. But we think the following steps will help you to complete that.

There are some steps to begin with:

1) The theme should first be customized in pages that means you have to separate your work through pages. i.e. Home page, product view page, contact us page.

2)some pages in Magento theme are CMS pages. That means they have to be designed from admin side only. After logging in your Magento you can go to the admin>CMS>pages.

3)from here after selecting any particular page you can redesign that page as your need of theme. You can also add attractive images. Only the content part of the page can be designed from here but not the header or footer section.

4)Magento is providing .phtml and .xml files for every pages. The work related to design is all done into .phtml file of a page. i.e. you want to redesign the product view page then you have to customize the file
App>design>frontend>your_theme_name>template>catalog>product>view.phtml

5)same way in the layout folder there are .xml files for each block that is called over the HTML page and we can see in the front end. If you want to enable or disable a block or want to change the place of block that can be done from here.

These are the steps to be followed nicely and you can implement your theme over Magento default theme.

Hope this will help..

Basics to create a custom module in magento

1. How to create a module

MODULE NAMESPACE

The first directory we will create is a “namespace.” This can be called anything you like, but the convention is some form of the name of the company or module’s author.
So, create the directory app/code/local/[namespace]

MODULE NAME
Create the directory app/code/local/[namespace]/[module name].
Module name should be according to functionality of our module.
We should now have the following directory structure for our module. These directory and file names are case-sensitive, so capitalize where appropriate.


app
  - code
      - local
          - [namespace]
              - [modulename]

2. Configure our module

Next, we will begin to configure our module. The configuration files belong inside our module in a directory named etc.
app/code/local/[namespace]/[modulename]/etc/config.xml.
Now, we will create a simple config.xml file, which contains following code:

<?xml version="1.0" encoding="UTF-8"?>
	
	<!-- The root node for Magento module configuration -->
	<config> 
	 
	    <!-- 
	        The module's node contains basic 
	        information about each Magento module
	    -->
	    <modules>
	 
	        <!--
	            This must exactly match the namespace and module's folder
	            names, with directory separators replaced by underscores
	        -->
	        <namespace_modulename>
	 
	            <!-- The version of our module, starting at 0.0.1 -->
	            <version>0.0.1</version>
	 
	        </ namespace_modulename >
	 
	    </modules>
	 
	</config>

3. Activating Our Module

The next step is to inform our Magneto installation that our module exists, which we do by creating a new XML file in app/etc/modules.
The name of this XML file can be anything you like but it is preferable that we use like below
Let’s create app/etc/modules/[namespace_modulename].xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <namespace_modulename>

            <!-- Whether our module is active: true or false -->
            <active>true</active>

            <!-- Which code pool to use: core, community or local -->
            <codePool>local</codePool>

        </namespace_modulename>
    </modules>
</config>

MYSQL: Solve the error related to SQL_BIG_SELECTS

Hello friends,
In our site we had a PHP script page which executes a MYSQL database query to fetch records and display report.
That page worked fine for few months, but since last few days it started giving an error message:

The SELECT would examine too many records and probably take a very long time. Check your WHERE and use SET OPTION SQL_BIG_SELECTS=1 if the SELECT is ok

Now to solve the above error I searched the web and found a proper solution, which I would like to discuss here.
The error had occurred because in our MYSQL server SQL_BIG_SELECTS variable was turned off.
Some basic information about SQL_BIG_SELECTS variable is as under:
1. SQL_BIG_SELECTS variable prevents users from accidentally executing unnecessary large queries.
2. MYSQL determines whether or not a query is large based on the value of MAX_JOIN_SIZE variable. If the query is examining more than this number of rows, it will consider it as a big select or a large query. We can execute SHOW VARIABLES command to view values of MYSQL server variables.
3. I believe that indexing and good WHERE clause can prevent this problem.
Now if we want to turn on the SQL_BIG_SELECTS variable then we can do it from my.cnf file or at server startup. But some VPS (Virtual Private Server) servers do not allow us to do that.
So as an alternate solution I executed

mysql_query("SET OPTION SQL_BIG_SELECTS = 1");

query in our PHP script just after the database connection, and it works fine.
The solution works fine. But this could be said a temporary solution, because some servers don’t provide enough permissions. If the sever provides it you can change the my.cnf file or at server startup and implement it permanently.
Otherwise implement the above given solution. It remains in effect during the current database session between our PHP application and the database. So we have to execute above query after each database connection.
Hope this will help you.
Thanks.