首页 > 编程技术 > php

利用php header函数实现文件下载保存到本地

发布时间:2016-11-25 15:51

header() 函数向客户端发送原始的 http 报头。

认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 php教程 4 以及更高的版本中,您可以使用输出缓存来解决此问题):

<html>
<?php
// 结果出错
// 在调用 header() 之前已存在输出
header('location: http://www.111cn.net/');
?>语法
header(string,replace,http_response_code)参数 描述
string 必需。规定要发送的报头字符串。
replace 可选。指示该报头是否替换之前的报头,或添加第二个报头。

默认是 true(替换)。false(允许相同类型的多个报头)。
 
http_response_code 可选。把 http 响应代码强制为指定的值。(php 4 以及更高版本可用)

 

<?php
function downfile()
{

 $filename=realpath("resume.html");
 header( "content-type:   application/octet-stream ");
 header( "accept-ranges:   bytes ");
    header( "accept-length: " .filesize($filename));
 header( "content-disposition:   attachment;   filename= 4.html");
 echo file_get_contents($filename);
 readfile($filename);
}
downfile();

?>

 

<?php

function downfile($fileurl)
{
$filename=$fileurl;
$file   =   fopen($filename, "rb");
header( "content-type:   application/octet-stream ");
header( "accept-ranges:   bytes ");
header( "content-disposition:   attachment;   filename= 4.doc");


$contents = "";
while (!feof($file)) {
  $contents .= fread($file, 8192);
}
echo $contents;
fclose($file);

}
$url=$_request['url'];
$url="http://www.111cn.net";
downfile($url);

?>

// date_format2($rs['time'],'%y年%m月%d日%h时%m分%s秒');
function date_format2($string, $format='%b %e, %y', $default_date=null)
{
    if (substr(php教程_os,0,3) == 'win') {
           $_win_from = array ('%e',  '%t',       '%d');
           $_win_to   = array ('%#d', '%h:%m:%s', '%m/%d/%y');
           $format = str_replace($_win_from, $_win_to, $format);
    }
    if($string != '') {
        return strftime($format, smarty_make_timestamp($string));
    } elseif (isset($default_date) && $default_date != '') {
        return strftime($format, smarty_make_timestamp($default_date));
    } else {
        return;
    }
}
function smarty_make_timestamp($string){
    if(empty($string)) {
        $string = "now";
    }
    $time = strtotime($string);
    if (is_numeric($time) && $time != -1)
        return $time;
    if (preg_match('/^d{14}$/', $string)) {
        $time = mktime(substr($string,8,2),substr($string,10,2),substr($string,12,2),
               substr($string,4,2),substr($string,6,2),substr($string,0,4));

        return $time;
    }
    $time = (int) $string;
    if ($time > 0)
        return $time;
    else
        return time();
}

一个很简单的应用实例,就利用了jquery.ajax来实现无刷新登录效果了,方便很简单扼要,实例也只讲了jquery应用这一圬,php教程那款就没讲了,有需要的朋友可以参考一下。

<script" width=100% src="js/jquery-1.4.2.js" type="text/网页特效"></script>
<script language="javascript">
function test() {
$.ajax({
type: "post",
url: "ap.asp教程x",
data: "username=123&password=456" ,
success: function(data) {
if (data == "true") {
alert("登陆成功!");
}
else {
alert("登陆失败");
}
}
});
}
function btntestajax_onclick() {
test();
}
</script>


html代码

<form id="form1" runat="server">
<div>
<input id="btntestajax" type="button" value="测试异步刷新" onclick="return btntestajax_onclick()" /></div>
</form>

ap.php文件

<?
 echo true;
?>

http_referer ,最简单的图片仿盗就是利用php教程的这个超级全局变量来实例了,但这个函数我们可以很简单的破解,原因我们可以写一个类是于浏览器的相关信息发送。代码如下。

function getremotefile($url, $refer = '') {
$option = array(
'http' => array(
'header' => "referer:$refer")
);
$context = stream_context_create($option);
return file_get_contents($url, false, $context);
}

stream_context_create创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。

函数原型:resource stream_context_create ([ array $options [, array $params ]] )


更多详细内容请查看:http://www.111cn.net/phper/php-function/36535.htm

zend framework invalid command 'rewriteengine'解决办法

zend framework  tutorial时候,运行例子,浏览器报错为:
the server encountered an internal error and was unable to complete your request. either the server is overloaded or there was an error in a cgi script.


apache error.log 中出现如下错误:
invalid command 'rewriteengine', perhaps教程 mis-spelled or defined by a module not included in the server configuration

解决办法:

打开httpd.conf ,取消 loadmodule rewrite_module modules/mod_rewrite.so前的注释

 

标签:[!--infotagslink--]

您可能感兴趣的文章: