Posts Tagged ‘PHP’

How to force download in php

Create a new php file and copy and paste following script, then change the filename of the file to be downloaded.

  1. <?PHP
  2.  
  3. // HTTP Header
  4. header('Content-disposition: attachment; filename=applicationform.pdf');
  5.  
  6. // MIME0type
  7. header('Content-type: application/pdf');
  8.  
  9. // read the file
  10.  
  11. readfile('applicationform.pdf');

References : How To Use PHP to Force a File Download

,

No Comments


Save, resize image using php
image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }
   }
   function getWidth() {
      return imagesx($this->image);
   }
   function getHeight() {
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }
}
?>

source

, , , , , ,

No Comments


How to access variables or methods of the parent class in php

use the double colon(::) and parent keyword to access the variables or methods of the parent class in php

  1. class myparent {
  2. function myfunction() {
  3. echo "parent method";
  4. }
  5. }
  6.  
  7. class mychild extends myparent {
  8. function myfunc()[
  9. parent::myfunction();
  10. }
  11. }

, , , ,

No Comments


How to access Static variables in php

To access Static variables;

  1. class mystatic {
  2. private static $staticvar;
  3.  
  4. public static function staticmethod{
  5. return <strong>self::$staticvar</strong>;
  6. }
  7. }

, ,

No Comments


How to generate random password in php
  1. function get_randomString($count=8, $rm_similar = false){
  2.  //create list of characters
  3.  
  4.  $chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));
  5.  
  6.  // remove similar looking characters that might cause confusion
  7.  if ($rm_similar) {
  8.   unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8], $chars['B'],
  9.     $chars['I'], $chars['O'], $chars['Q'], $chars['S'], $chars['U'], $chars['V'], $chars['Z']);  
  10.  }
  11.  
  12.  // generate the string of random text
  13.  
  14.  for( $i = 0, $text = '';  $i < $count; $i++ ) {
  15.   $text .= array_rand($chars);
  16.  }
  17.  return $text;
  18. }

,

No Comments


What @ symbol in PHP

@ symbol – is used to suppress warning message.

,

No Comments


Accessing Static Variable within Static method in a Class

Use self to access static variable or const within a class.
example:

  1. class static_class {
  2.  
  3.             private static static_variable = 'I am static ';
  4.             public static function getStaticVariable() {
  5.                  self::$static_variable;
  6.             }
  7. }

, , ,

No Comments


How to display array as Tree
  1. <?php
  2. $customers
  3. = array(
  4. array('first' => 'Bill', 'last' => 'Jones',
  5. 'age' => 24, 'state' => 'CA'),
  6. array('first' => 'Mary', 'last' => 'Smith',
  7. 'age' => 32, 'state' => 'OH'),
  8. array('first' => 'Joyce', 'last' => 'Johnson',
  9. 'age' => 21, 'state' => 'TX'),
  10. );
  11. printf("print_r():<pre>%s</ pre>", print_r($customers, TRUE));
  12. printf("var_export():<pre>%s</ pre>", var_export($customers, TRUE));
  13. print 'var_dump():<pre>';
  14. var_dump($customers);
  15. print '</ pre>';
  16. ?>

,

No Comments


How to Remove Item in Array in PHP

Use unset() to remove an item/element in Arrays, see example below

  1. <?PHP
  2.  
  3. $array = array('name'=>'Bill', 'age'=>32, 1=>'25 Main St.',
  4. 2=>'Apt. 24', 'City'=>'San Francisco', 'state'=>'CA');
  5.  
  6. printf("<pre>;%s</ pre>\n", var_export($array, TRUE));
  7.  
  8. unset($array['age']);
  9.  
  10. printf("<pre>%s</ pre>\n", var_export($array, TRUE));
  11.  
  12. ?>

, ,

No Comments


Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0

Error: Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0

This means the current user or user the service has no read/write permission on the folder.

Type the following command to change permission

chmod a+rwx /path/to/folder

Read More
chmod tutorial

, ,

No Comments



SetPageWidth