All posts by WhiteWind

About WhiteWind

I'm a Perl-PHP Developer from India. I'm a great fan of Wordpress & Perl.

Script to download a file in PHP

To make a file to download in php, we can use the following code :

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file_name));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_name));
ob_clean();
flush();
readfile($file_name);

Linux System Command to Zip Files

The linux system command to create zip single or multiple files are as follows :

zip zip_file_name.zip file_name1.ext file_name2.ext file_name3.ext

If the files are in another folder and you see those file names if the downloaded zip file in which you wish not to see these files, then use the following command :

zip zip_file_name.zip file_name1.ext file_name2.ext file_name3.ext

How can we redeclare a class safely in PHP?

Basically PHP doesn’t allow re-declare a class directly. If you are in need to get re-declare a class in php, then I suggest you to write that class in a separate file and use require_one to call that file to the desired page. It’s as follows:

Page1.php

class abcd
{
    function max()
    {
        echo "Hello World!!! count:-".$GLOBALS['x'];
    }
}

Page2.php

$i=10; 
for($x=0;$x<$i;$x++)
{
      require_once "Page1.php";
      $myclass = new abcd();
      $myclass->max();
}

Now it will work as you desired. It worked for me.

The output will be as follows :

 Hello World!!! count:- 0
 Hello World!!! count:- 1
 Hello World!!! count:- 2
 Hello World!!! count:- 3
 Hello World!!! count:- 4
 Hello World!!! count:- 5
 Hello World!!! count:- 6
 Hello World!!! count:- 7
 Hello World!!! count:- 8
 Hello World!!! count:- 9

How to split a group of numbers to an array in PHP?

To split a number 187954 to an array as follows:

<?php
          $x=187954;
          $y=(string)$x;
          echo $y[0]."<br/>";  //1
          echo $y[1]."<br/>";  //8
          echo $y[2]."<br/>";  //7
          echo $y[3]."<br/>";  //9
          echo $y[4]."<br/>";  //5
          echo $y[5]."<br/>";  //4
?>

The Output will be as follows:

    1
    8
    7
    9
    5
    4

Advanced mail sending method in PHP

It’s as follows :

Page 1:config.php
class mymailer
    {
        function mysendmail($from,$to,$subject='sub',$body='msg',$content_type='text/html',$auth=true,$host=MAIL_HOST,$username=MAIL_USERNAME,$password=MAIL_PASSWORD)
        {
            require_once "Mail.php";   //you should install PEAR::Mail module in your server
            $headers = array ('From' => $from,
                'To' => $to,
                'Subject' => $subject,
                'Content-type'=>$content_type
            );
            $smtp = Mail::factory('smtp',
                array ('host' => $host,
                'auth' => $auth,
                'username' => $username,
                'password' => $password)
            );
            $mail = $smtp->send($to, $headers,$body);
            if (PEAR::isError($mail))
            {
                //return $mail->getMessage();
                return 'F';
            }
            else
            {
                return 'S';
            }
        }
    }

You can call this class as :

Page 2: mail_sending_page.php
require_once "config.php";
$from="frommail@company.com";
$to="tomail@example.com";
$subject="My Mail Subject";
$body="This is the body of this mail. Your content goes here.";
$mails=new mymailer;
$mails->mysendmail($from,$to,$subject,$body);