php文件下载代码,如下:
<?php
/**
* 文件下载
* 多种文件格式,包括pdf、zip、gif、jpg、mpeg、word等。
* edit www.
*/
function dl_file($file){
//First, see if the file exists
if (!is_file($file)) { die("<b>404 File not found!</b>"); }
//Gather relevent info about file
$len = filesize($file);
$filename = basename($file);
$file_extension = strtolower()(substr(strrchr($filename,"."),1));
//This will set the Content-Type to the appropriate setting for the file
switch( $file_extension ) {
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/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
case "mp3": $ctype="audio/mpeg"; break;
case "wav": $ctype="audio/x-wav"; break;
case "mpeg":
case "mpg":
case "mpe": $ctype="video/mpeg"; break;
case "mov": $ctype="video/quicktime"; break;
case "avi": $ctype="video/x-msvideo"; break;
//The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
case "php":
case "htm":
case "html":
case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;
default: $ctype="application/force-download";
}
//Begin writing headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
//Use the switch-generated Content-Type
header("Content-Type: $ctype");
//Force the download
$header="Content-Disposition: attachment; filename=".$filename.";";
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len);
@readfile($file);
exit;
}
?>
1,html内容部分
2,文件get.php,内容如下:
<?php
/**
* php实现文件下载
* edit www.
*/
if (!isset()(GET["file"]) || !isset(GET["type"])) {
print "no file selsect"; exit();
}
$file = GET["file"].".".GET["type"];
if (@$fp = fopen($file,'r')){
header ("Content-type: octet/stream");
if (strstr(SERVER["HTTP_USER_AGENT"], "MSIE")){
header("Content-Disposition: filename=".mb_convert_encoding('nginx中文手册.doc','GB2312','UTF-8')); // For IE
}else{
header("Content-Disposition: attachment; filename=".mb_convert_encoding('nginx中文手册.doc','GB2312','UTF-8')); // For Other browsers
} while(!@feof($fp)){
echo fread($fp,1024);
}
//@fpassthru($fp);
exit();
} else{
print "sorry,file not exists!";
}
?>
1,PHP解析JSON数据
$obj=json_decode()($json_string);
echo $obj->name; //prints foo
echo $obj->interest[1]; //prints php
2,PHP解析XML 数据
$xml_string="<?xml version='1.0'?>
<users>
<user id='123'>
<name>Test</name>
<email>test@</name>
</user>
<user id='345'>
<name>News</name>
<email>fnews@xxx.net</name>
</user>
</users>";
//load the xml string using simplexml
$xml = simplexml_load_string($xml_string);
//loop through the each node of user
foreach ($xml->user as $user)
{
//access attribute
echo $user['id'], ' ';
//subnodes are accessed by -> operator
echo $user->name, ' ';
echo $user->email, '<br />';
}
如今,json在数据传输上的应用越来越广了,建议大家好好掌握下。