Archive for the ‘PHP’ Category
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
The following text is not allowed in this context:
This exception occurs when there is an extra string / character outside the element of the schema,
example:

Windows7 error access forbidden with xampp
goto windows\system32\drivers\etc\hosts, then, move the line with # ::1 localhost to the bottom
or
- Navigate to your xampp\apache\conf\extra folder
- Using a text editor, open httpd-xampp.conf
- Find the following line:
Allow from localhost
- Change the line to:
Allow from 127.0.0.1
- Save the file and restart your XAMPP server
- Try to access http://localhost/security/lang.php?en
You should now have no problems accessing the XAMPP Security Page via localhost or 127.0.0.1
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;
-
}
Difference between $this vs self in class using php
$this – use this if you are accessing a variable or method without static keyword within a class
self - use this if you are accessing a static variable or constant within a class
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;
-
}
-
}