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.
-
<?PHP
-
-
// HTTP Header
-
header('Content-disposition: attachment; filename=applicationform.pdf');
-
-
// MIME0type
-
header('Content-type: application/pdf');
-
-
// read the file
-
-
readfile('applicationform.pdf');
References : How To Use PHP to Force a File Download
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;
}
}
?>
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
-
class myparent {
-
function myfunction() {
-
echo "parent method";
-
}
-
}
-
-
class mychild extends myparent {
-
function myfunc()[
-
parent::myfunction();
-
}
-
}
How to access Static variables in php
To access Static variables;
-
class mystatic {
-
private static $staticvar;
-
-
public static function staticmethod{
-
return <strong>self::$staticvar</strong>;
-
}
-
}
How to generate random password in php
-
function get_randomString($count=8, $rm_similar = false){
-
//create list of characters
-
-
$chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));
-
-
// remove similar looking characters that might cause confusion
-
if ($rm_similar) {
-
unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8], $chars['B'],
-
$chars['I'], $chars['O'], $chars['Q'], $chars['S'], $chars['U'], $chars['V'], $chars['Z']);
-
}
-
-
// generate the string of random text
-
-
for( $i = 0, $text = ''; $i < $count; $i++ ) {
-
$text .= array_rand($chars);
-
}
-
return $text;
-
}
Accessing Static Variable within Static method in a Class
Use self to access static variable or const within a class.
example:
-
class static_class {
-
-
private static static_variable = 'I am static ';
-
public static function getStaticVariable() {
-
self::$static_variable;
-
}
-
}
How to display array as Tree
-
<?php
-
$customers
-
= array(
-
array('first' => 'Bill', 'last' => 'Jones',
-
'age' => 24, 'state' => 'CA'),
-
array('first' => 'Mary', 'last' => 'Smith',
-
'age' => 32, 'state' => 'OH'),
-
array('first' => 'Joyce', 'last' => 'Johnson',
-
'age' => 21, 'state' => 'TX'),
-
);
-
printf("print_r():<pre>%s</ pre>", print_r($customers, TRUE));
-
printf("var_export():<pre>%s</ pre>", var_export($customers, TRUE));
-
print 'var_dump():<pre>';
-
var_dump($customers);
-
print '</ pre>';
-
?>
How to Remove Item in Array in PHP
Use unset() to remove an item/element in Arrays, see example below
-
<?PHP
-
-
$array = array('name'=>'Bill', 'age'=>32, 1=>'25 Main St.',
-
2=>'Apt. 24', 'City'=>'San Francisco', 'state'=>'CA');
-
-
printf("<pre>;%s</ pre>\n", var_export($array, TRUE));
-
-
unset($array['age']);
-
-
printf("<pre>%s</ pre>\n", var_export($array, TRUE));
-
-
?>
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