Download File in PHP

Today, i have got new issue while downloading document in IE browser. Below is the message i got when i try to download a file in IE from https.

Internet Explorer cannot download file from server. Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be….

I spent more times to figure out the issue and even i changed the settings in IE but no use, still having the same issue. Its working fine in FF and other browsers.

At last i found the issue its because of PHP headers which i used in my code. I searched google and found this code which support in all browsers.

function downloadFile( $fullPath ){

// Must be fresh start
if( headers_sent() )
die(‘Headers Sent’);

// Required for some browsers
ini_set(‘zlib.output_compression’, ‘Off’);

// File Exists?
if( file_exists($fullPath) ){

// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts[“extension”]);

// Determine Content Type
switch ($ext) {
case “pdf”: $ctype=”application/pdf”; break;
case “exe”: $ctype=”application/octet-stream”; break;
case “zip”: $ctype=”application/zip”; break;
case “doc”: $ctype=”application/msword”; break;
case “xls”: $ctype=”application/”; break;
case “ppt”: $ctype=”application/”; break;
case “gif”: $ctype=”image/gif”; break;
case “png”: $ctype=”image/png”; break;
case “jpeg”:
case “jpg”: $ctype=”image/jpg”; break;
default: $ctype=”application/force-download”;

header(“Pragma: public”); // required
header(“Expires: 0”);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0”);
header(“Cache-Control: private”,false); // required for certain browsers
header(“Content-Type: $ctype”);
header(“Content-Disposition: attachment; filename=\””.basename($fullPath).”\”;” );
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: “.$fsize);
readfile( $fullPath );

} else
die(‘File Not Found’);


Hope this will be useful for those having similar problems.


Here’s another PHP function I cannot live without – I add this into my standard PHP functions include on all projects. Often when displaying text, I am forced to abbreviate the text to a certain number of characters. You might jump in a decide to use substring() on your text to achieve this abbreviation, but that can cause several problems. The most blatant is that you will often split the text right in the middle of a word. In addition, if there are any HTML tags in the text, they could get cut in the middle too, or have the closing tag left off completely, potentially breaking the display or exposing the remaining part of the tag.

As a solution to these problems, I have written a function that only trims on the last space before the number of characters you specify, so it will never cut words in half. Also, it strips out HTML tags before doing the character trim, preventing possible display issues. And, as a convenience it adds ellipses (the …) to all trimmed text, as a visual cue to the reader that the text has been abbreviated.


 * trims text to a space then adds ellipses if desired
 * @param string $input text to trim
 * @param int $length in characters to trim to
 * @param bool $ellipses if ellipses (...) are to be added
 * @param bool $strip_html if html tags are to be stripped
 * @return string
function trim_text($input, $length, $ellipses = true, $strip_html = true) {
    //strip tags, if desired
    if ($strip_html) {
        $input = strip_tags($input);
    //no need to trim, already shorter than trim length
    if (strlen($input) <= $length) {
        return $input;
    //find last space within length
    $last_space = strrpos(substr($input, 0, $length), ' ');
    $trimmed_text = substr($input, 0, $last_space);
    //add ellipses (...)
    if ($ellipses) {
        $trimmed_text .= '...';
    return $trimmed_text;
Author: Elliott Brueggeman

Today, we have a big problem to figure out in creating a particular database schema for your application.

While surfying internet, i found a great resource which has huge collections of databaes models. You can find of hundreds of sample database schemas on varies topics like Airline Reservations, Car Servicing, Pizza delivery, Telephone bills, Blood Bank, etc…

So when you creating a new database, i would recommend you to check out this site to get a better idea.

A sample schema for modeling “Blood Bank” can be seen below,

Blood Bank Database Schema

Blood Bank Database Schema

What’s new in PHP 5.4?

The process to release PHP 5.4 is moving on.

As you probably know, the updates that were intended for postponed PHP 6 were added to PHP 5.4.0 instead, so now PHP includes a huge set of new language features and removes several legacy behaviors.

Because of that I created a list of major changes since PHP 5.3 ,along with some examples and brief descriptions of these changes…

Major PHP improvements

Changes since PHP 5.3 version include:

  1. Added class member access on instantiation.Now you can use fluent interfaces like in Java:
    $myCar = (new Car)->setSpeed(100)->setColor('blue');
  2. Added callable typehint.This typehint allows a string with a function name, a closure, and an array composed of classname (or object) with method name.
    function foo(callable $cb) {
  3. Added closure rebinding as parameter to bindTo.Closure::bindTo() has been modified so now it accepts another argument that defines the new scope. This can either be an object if its class is used as the scope, or a class name.
    class A {
        private $x;
        public function __construct($v) {
            $this->x = $v;
        public function getIncrementor() {
            return function() { return ++$this->x; };
    class B extends A {
        private $x;
        public function __construct($v) {
            $this->x = $v*2;
    $a = new A(0);
    $b = new B(10);
    $ca = $a->getIncrementor();
    echo "Testing with scope given as object", "\n";
    $cb = $ca->bindTo($b, $b);
    $cb2 = Closure::bind($ca, $b, $b);
    echo "Testing with scope as string", "\n";
    $cb = $ca->bindTo($b, 'B');
    $cb2 = Closure::bind($ca, $b, 'B');
    $cb = $ca->bindTo($b, NULL);


    Testing with scope given as object
    Testing with scope as string
  4. Added short array syntax.Makes PHP code more readable and maintainable.
    $a = [1, 2, 3];
    $b = ['foo' => 'orange', 'bar' => 'apple', 'baz' => 'lemon'];
  5. Added binary value format.Now it’s possible to use binary values directly in the PHP code:
    $x = 0b001110;
    echo $x;
  6. Added support for Class::{expr}() syntax.Makes PHP more flexible, when calling class/object methods.
    $method = 'method';
    $test = new Test();


  7. Added support for Traits.A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.
    class Base {
        public function sayHello() {
            echo 'Hello ';
    trait SayWorld {
        public function sayHello() {
            echo 'World!';
    class MyHelloWorld extends Base {
        use SayWorld;
    $o = new MyHelloWorld();


    Hello World!
  8. Added closure $this support back.Now you have an access to every object property (be it public or not).
    class A {
        private $value = 1;
        function firstGetter($name) {
            return function() use ($name) {
                return $this->$name;
        function secondGetter() {
            return function($name)  {
                return $this->$name;
    $a = new A();
    $firstGetter = $a->firstGetter('value');
    echo $firstGetter();
    $secondGetter = $a->secondGetter();
    echo $secondGetter('value');


  9. Added array dereferencing support.Provides the implementation of array dereferencing of method/function return.
    function fruit () {
        return array('a' => 'apple', 'b' => 'banana');
    echo fruit()['a'];


  10. Added indirect method call through array.Now $foo() also works in the cases where $foo is a callable array or Closure object.
    class Hello {
        static public function world($x) {
            echo "Hello, $x\n";
    function helloWorld($x) {
        echo "Hello, $x\n";
    $callbacks = array(
        array('Hello', 'world'),
        function ($x) { echo "Hello, $x\n"; },
    foreach ($callbacks as $k => $callback) {
        if (is_callable($callback)) {


    Hello, 0
    Hello, 1
    Hello, 2
  11. Changed $GLOBALS into a JIT autoglobal.$GLOBALS array is initialized only if it’s used. This is a performance/memory optimization, it can however break some of the existing scripts or opcode caches.
  12. Improved performance of @ (silence) operator.This can speed up PHP scripts which rely heavily on a silence operator, for example:
    $x = @file_get_contents('/etc/passwd');
    echo $x;
  13. Added multibyte support by default.Previously php had to be compiled with –enable-zend-multibyte. Now it can be enabled or disabled through zend.multibyte directive in php.ini.
  14. Added built-in web server that is intended for testing purpose.The following command will open a web server on the port 8000.
    php -S localhost:8000
  15. Lots of performance and memory usage improvements

Removed major PHP features

  1. Removed break/continue $var syntax.You can no longer use variable to tell PHP how many levels of enclosing loops it should skip to the end of.
  2. Removed safe mode and all related ini options.Functionality described in this article and marked as depreciated in PHP 5.3 has now been removed
  3. Removed register_globals and register_long_arrays ini options.If enabled, register_globals injected PHP scripts with all sorts of variables, like request variables from HTML forms or values from GET requests. Now, every request/environment variable must be fetched from an appropriate PHP array.
  4. Removed allow_call_time_pass_reference option.Passing arguments by reference at function call time was deprecated for code-cleanliness reasons. A function can modify its arguments in an undocumented way if it didn’t declare that the argument shall be passed by reference. To prevent side-effects it’s better to specify which arguments are passed by reference in the function declaration only.

For a full list of changes in PHP 5.4, see the ChangeLog. For source downloads please visit QAT downloads page, Windows binaries can be found here.


Author: Artur Graniszewski