Step by Step Analysis solution PHP file download directly in the Web page to open the problem ____php

Source: Internet
Author: User
Tags testlink
	
"Preface"

Do not know when you look at the PHP function, there is no notice of such a sentence-

It is important to recognize that the header () function must be called before any actual output is sent

(in PHP 4 and later, you can use output caching to resolve this issue)


Hey ~ ~ a problem that bothered me for a day, and ended up with

This sentence ends.


"Body"


In the use of Testlink, found in the Testlink attachment in the selection of downloads are directly in the Web page open, pictures, Word are displayed garbled. Various puzzles, began the following research:


1 To see the code, no clue.


I directly check the code in the PHP code to implement the download, to see if the header is correct:

@ob_end_clean (); Require_once ('.. /..
/config.inc.php '); Require_once ('..
/functions/common.php '); Require_once ('..
/functions/attachments.inc.php ');

Testlinkinitpage ($db, False,false, "checkrights");
$args = Init_args ();
	if ($args->id) {$attachmentRepository = Tlattachmentrepository::create ($db);
	$attachmentInfo = $attachmentRepository->getattachmentinfo ($args->id); if ($attachmentInfo && checkattachmentid ($db, $args->id, $attachmentInfo)) {$content = $
		Attachmentrepository->getattachmentcontent ($args->id, $attachmentInfo);
			if ($content!= "") {@ob_end_clean ();
			Header (' Pragma:public ');
			Header ("Cache-control:"); if (!) ( Isset ($_server["https"]) && $_server["https"] = = "On" && Preg_match ("/msie/", $_server["Http_user_")
			AGENT "]) header (' Pragma:no-cache ');
			Header (' Content-type: '. $attachmentInfo [' File_type ']);
			Header (' Content-length: '. $attachmentInfo [' file_size ']); Header ("content-disposition:attachment; Filename=\ "{$attachmentInfo [' file_name ']}\ '];
			Header ("Content-description:download Data");
			Echo $content;
		Exit (); }
	}
}


2 various attempts, but the opposite.


Read the code first reaction is right AH.

Header ("content-disposition:attachment; Filename=\ "{$attachmentInfo [' file_name ']}\"); This file is named after the attachment instead of opening the header directly in the Web page
(' Content-type: '. $attachmentInfo [' File_type ']);/This is also or goes to the type of file

The next reaction is, is it content-type problem, let us direct hard code to become a binary flow try it

Header (' Content-type: ' Application/octet-stream);
Header ("content-disposition:attachment; Filename=\ "{$attachmentInfo [' file_name ']}\");


The result is very disappointing, the picture is still open in the browser, display garbled.
OK, I choose a nonexistent content-type try it, so the browser must not know. The result of the attempt, or failure.

Header (' Content-type: ' Application/download);

3 The mountain is not up to me, I will go to the mountains. all sorts of attempts were fruitless, and I began to wonder if the function of the header was wrong, so I started a new attempt to build a test.php file to test this download.

It turns out that test.php is running very normal, click the download link and download the picture correctly.

<?php
//File test.php
//================================= file Download
if ($_get[' down ']) {
    $file _name = " A.png ";
    $file _dir = "./";

    if (!file_exists ($file _dir. $file _name)) {//Check the file for the presence of
        echo "File not found";
        Exit;
    } else {
        //input file tag
        header ("Content-type:application/octet-stream");//specify that the following output characters will be saved in the client header in the form of a download file
        ( "Accept-ranges:bytes")//Specifies the size of the downloaded file
        Header ("Accept-length:"). FileSize ($file _dir. $file _name))//Specify the file size
        Header ("content-disposition:attachment; Filename= ". $file _name); Specifies the file name of the download. Extension
        $file = fopen ($file _dir. $file _name, "R");//Open File/
        output file contents, no other output
        from the page except download file encoding Echo fread ($file, FileSize ($file _dir. $file _name));
        Fclose ($file);
        Exit Prevent reading of other output
    }
}
?>


<a href= "? down=1" > Downloads </a>

4 from the cause, the green.


Why does the test program work while the previous program does not work?
With the Chrome dev tool, I got another interesting discovery-
No matter what content-type I set in the program, the Content-type doesn't actually work.


From the performance of the result, we can analyze it-we call the header does not take effect.
We go back and look at the source code, what's the problem.


Back to the preface.


It is important to recognize that the header () function must be called before any actual output is sent

(in PHP 4 and later, you can use output caching to resolve this issue)


It is strongly suspected that there is an early echo in the function Testlinkinitpage ($db, False,false, "checkrights"). So the header fails.

In this principle, I put the header life ahead, the program finally work.


@ob_end_clean ();
Header (' Pragma:public ');
Header ("Cache-control:"); Require_once ('.. /..
/config.inc.php '); Require_once ('..
/functions/common.php '); Require_once ('..
/functions/attachments.inc.php ');

Testlinkinitpage ($db, False,false, "checkrights");
$args = Init_args ();
	if ($args->id) {$attachmentRepository = Tlattachmentrepository::create ($db);
	$attachmentInfo = $attachmentRepository->getattachmentinfo ($args->id); if ($attachmentInfo && checkattachmentid ($db, $args->id, $attachmentInfo)) {$content = $
		Attachmentrepository->getattachmentcontent ($args->id, $attachmentInfo);
			
			if ($content!= "") {@ob_end_clean (); if (!) ( Isset ($_server["https"]) && $_server["https"] = = "On" && Preg_match ("/msie/", $_server["Http_user_")
			AGENT "]) header (' Pragma:no-cache ');
			Header (' Content-type: '. $attachmentInfo [' File_type ']);
			Header (' Content-length: '. $attachmentInfo [' file_size ']); Header ("content-disposition:attachment; Filename=\ "{$attachmentinfo[' file_name ']}\ "");
			Header ("Content-description:download Data");
			Echo $content;
		Exit (); }
	}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.