当前位置: 编程技术>php
本页文章导读:
▪php跨浏览器下载文件时中文乱码问题的解决方法 代码如下:
代码示例:
<?php
/**
* 解决跨浏览器下载文件,中文乱码的问题
* edit www.
*/
$ua = $_SERVER["HTTP_USER_AGENT"];
$filename = "中文 文件名.txt";
$encoded_filename = urlencode($filename);
$encoded_fi.........
▪php安全下载大文件的实现代码 先来看下一般文件的安全下载方法:
代码示例:
<?php
/**
* 一般文件安全下载
* edit www.
*/
$durl = 'file/phpcms2008_o2abf32efj883c91a.iso';
$filename = 'phpcms2008_o2abf32efj883c91a.iso';
.........
▪php readfile函数下载文件并判断权限的代码示例 代码如下:
代码示例:
<?php
/**
* header与readfile函数应用举例
* 下载文件 判断权限
* edit www.
*/
$file = get_file_address();// 文件的真实地址(支持url,不过不建议用url)
if (file_exists($file.........
[1]php跨浏览器下载文件时中文乱码问题的解决方法
来源: 互联网 发布时间: 2013-12-24
代码如下:
代码示例:
<?php
/**
* 解决跨浏览器下载文件,中文乱码的问题
* edit www.
*/
$ua = $_SERVER["HTTP_USER_AGENT"];
$filename = "中文 文件名.txt";
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace()("+", "%20", $encoded_filename);
header('Content-Type: application/octet-stream');
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
print 'ABC';
?>
[2]php安全下载大文件的实现代码
来源: 互联网 发布时间: 2013-12-24
先来看下一般文件的安全下载方法:
代码示例:
<?php
/**
* 一般文件安全下载
* edit www.
*/
$durl = 'file/phpcms2008_o2abf32efj883c91a.iso';
$filename = 'phpcms2008_o2abf32efj883c91a.iso';
$file = @fopen($durl, 'r');
header("Content-Type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize($durl));
header("Content-Disposition: attachment; filename=".$filename);
echo fread($file,filesize($durl));
fclose($file);
?>
/**
* 一般文件安全下载
* edit www.
*/
$durl = 'file/phpcms2008_o2abf32efj883c91a.iso';
$filename = 'phpcms2008_o2abf32efj883c91a.iso';
$file = @fopen($durl, 'r');
header("Content-Type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize($durl));
header("Content-Disposition: attachment; filename=".$filename);
echo fread($file,filesize($durl));
fclose($file);
?>
以上的代码,在遇到大文件时,超过php.ini配置的内存最大值的文件时,服务器会非常占用CPU资源,文件不能正常下载,只能下载几十Kb的文件。
可以用下面的代码来解决:
代码示例:
<?php
/**
* 大文件安全下载的实现代码
* edit www.
*/
function download($url, $filename) {
// 获得文件大小, 防止超过2G的文件, 用sprintf()来读
$filesize = sprintf ( "%u", filesize ( $url ) );
if (! $filesize) {
return;
}
header ( "Content-type:application/octet-stream\n" ); //application/octet-stream
header ( "Content-type:unknown/unknown;" );
header ( "Content-disposition: attachment; filename=\"" . $filename . "\"" );
header ( 'Content-transfer-encoding: binary' );
if ($range = getenv ( 'HTTP_RANGE' )) { // 当有偏移量的时候,采用206的断点续传头
$range = explode() ( '=', $range );
$range = $range [1];
header ( "HTTP/1.1 206 Partial Content" );
header ( "Date: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s", filemtime ( $url ) ) . " GMT" );
header ( "Accept-Ranges: bytes" );
header ( "Content-Length:" . ($filesize - $range) );
header ( "Content-Range: bytes " . $range . ($filesize - 1) . "/" . $filesize );
header ( "Connection: close" . "\n\n" );
} else {
header ( "Content-Length:" . $filesize . "\n\n" );
$range = 0;
}
loadFile ( $url );
}
function loadFile($filename, $retbytes = true) {
$buffer = '';
$cnt = 0;
$handle = fopen ( $filename, 'rb' );
if ($handle === false) {
return false;
}
while ( ! feof ( $handle ) ) {
$buffer = fread ( $handle, 1024 * 1024 );
echo $buffer;
ob_flush ();
flush ();
if ($retbytes) {
$cnt += strlen ( $buffer );
}
}
$status = fclose ( $handle );
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
?>
/**
* 大文件安全下载的实现代码
* edit www.
*/
function download($url, $filename) {
// 获得文件大小, 防止超过2G的文件, 用sprintf()来读
$filesize = sprintf ( "%u", filesize ( $url ) );
if (! $filesize) {
return;
}
header ( "Content-type:application/octet-stream\n" ); //application/octet-stream
header ( "Content-type:unknown/unknown;" );
header ( "Content-disposition: attachment; filename=\"" . $filename . "\"" );
header ( 'Content-transfer-encoding: binary' );
if ($range = getenv ( 'HTTP_RANGE' )) { // 当有偏移量的时候,采用206的断点续传头
$range = explode() ( '=', $range );
$range = $range [1];
header ( "HTTP/1.1 206 Partial Content" );
header ( "Date: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s", filemtime ( $url ) ) . " GMT" );
header ( "Accept-Ranges: bytes" );
header ( "Content-Length:" . ($filesize - $range) );
header ( "Content-Range: bytes " . $range . ($filesize - 1) . "/" . $filesize );
header ( "Connection: close" . "\n\n" );
} else {
header ( "Content-Length:" . $filesize . "\n\n" );
$range = 0;
}
loadFile ( $url );
}
function loadFile($filename, $retbytes = true) {
$buffer = '';
$cnt = 0;
$handle = fopen ( $filename, 'rb' );
if ($handle === false) {
return false;
}
while ( ! feof ( $handle ) ) {
$buffer = fread ( $handle, 1024 * 1024 );
echo $buffer;
ob_flush ();
flush ();
if ($retbytes) {
$cnt += strlen ( $buffer );
}
}
$status = fclose ( $handle );
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
?>
调用示例:
代码示例:
//大文件的安全下载
download($url, $filename);
download($url, $filename);
[3]php readfile函数下载文件并判断权限的代码示例
来源: 互联网 发布时间: 2013-12-24
代码如下:
代码示例:
<?php
/**
* header与readfile函数应用举例
* 下载文件 判断权限
* edit www.
*/
$file = get_file_address();// 文件的真实地址(支持url,不过不建议用url)
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean(); //注意此函数的调用,清空但不关闭输出缓存,否则下载的文件头两个字符会是0a
flush();
readfile($file); // 输出文件内容
}
?>
/**
* header与readfile函数应用举例
* 下载文件 判断权限
* edit www.
*/
$file = get_file_address();// 文件的真实地址(支持url,不过不建议用url)
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean(); //注意此函数的调用,清空但不关闭输出缓存,否则下载的文件头两个字符会是0a
flush();
readfile($file); // 输出文件内容
}
?>
最新技术文章: