圖形圖像處理技術,gd庫的強大支持,PHP的圖像可以是PHP的強項,PHP圖形化類庫,jpgraph是一款非常好用的強大的圖形處理工具。 在PHP中載入GD庫 gd官方網址下載: 激活gd庫,修改php.in文件 驗證GD庫是否安裝成功 輸入“127.0.0.1/phpinfo.php”並按Ente ...
圖形圖像處理技術,gd庫的強大支持,PHP的圖像可以是PHP的強項,PHP圖形化類庫,jpgraph是一款非常好用的強大的圖形處理工具。
在PHP中載入GD庫
gd官方網址下載:
http://www.boutell.com/gd
激活gd庫,修改php.in文件
將該文件中的“;extension=php_gd2.dll”選項前的分號“;”刪除
驗證GD庫是否安裝成功
輸入“127.0.0.1/phpinfo.php”並按Enter鍵,檢索到的安裝信息,即說明GD庫安裝成功。
Jpgraph的安裝與配置
官方網站http://www.aditus.nu/jpgraph/下載
解壓到文件夾,編輯php.ini文件,修改include_path參數,如include_path = ".;F:\AppServ\www\jpgraph",重新啟動Apache。
配置Jpgraph類庫的文件jpg-config.inc.php,
支持中文的配置
DEFINE('CHINESE_TTF_FONT','bkai00mp.ttf');
預設圖片格式的配置
DEFINE("DEFAULT_GFORMAT","auto");
創建畫布,可以通過imagecreate()函數實現
<?php
$im = imagecreate(200,100);
$white = imagecolorallocate($im, 255,65,150);
imagegif($im);
?>
gd庫支持中文,但只能是utf-8,使用imageString()會顯示亂碼,只能接收utf-8編碼格式,預設使用英文字體。
header()函數定義輸出圖像類型
imagecreatefromjpeg()函數載入圖片
imagecolorallocate()函數設置輸出字體顏色
iconv()函數對輸出的中文字元串的編碼格式進行轉換
imageTTFText()函數向照片中添加文字
<?php
header("content-type:image/jpeg");
$im = imagecreateformjpeg("images/photo.jpg");
$textcolor = imagecolorallocate($im, 35,35,23);
//定義字體
$fnt="c:/windows/fonts/simhei.ttf";
//定義輸出字體串
$motto = iconv("gb2312","utf-8","長白山");
// 寫文字
imageTTFText($im, 220,0,200,233, $textcolor, $fnt, $motto); // 寫TTF文字到圖中
// 簡歷里jpeg圖形
imageipeg($im);
imagedestory($im);
?>
使用圖像處理技術生成的驗證碼
<?php
session_start();
header("content-type:image/png"); //設置創建圖像的格式
$image_width=70; //設置圖像寬度
$image_height=18; //設置圖像高度
srand(microtime()*100000); //設置隨機數的種子
for($i=0;$i<4;$i++){ //迴圈輸出一個4位的隨機數
$new_number.=dechex(rand(0,15));
}
$_SESSION[check_checks]=$new_number; //將獲取的隨機數驗證碼寫入到SESSION變數中
$num_image=imagecreate($image_width,$image_height); //創建一個畫布
imagecolorallocate($num_image,255,255,255); //設置畫布的顏色
for($i=0;$i<strlen($_SESSION[check_checks]);$i++){ //迴圈讀取SESSION變數中的驗證碼
$font=mt_rand(3,5); //設置隨機的字體
$x=mt_rand(1,8)+$image_width*$i/4; //設置隨機字元所在位置的X坐標
$y=mt_rand(1,$image_height/4); //設置隨機字元所在位置的Y坐標
$color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); //設置字元的顏色
imagestring($num_image,$font,$x,$y,$_SESSION[check_checks][$i],$color); //水平輸出字元
}
imagepng($num_image); //生成PNG格式的圖像
imagedestroy($num_image); //釋放圖像資源
?>
<?php
session_start();
if($_POST["Submit"]!=""){
$checks=$_POST["checks"];
if($checks==""){
echo "<script> alert('驗證碼不能為空');window.location.href='index.php';</script>";
}
if($checks==$_SESSION[check_checks]){
echo "<script> alert('用戶登錄成功!');window.location.href='index.php';</script>";
}else{
echo "<script> alert('您輸入的驗證碼不正確!');window.location.href='index.php';</script>";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>驗證碼應用</title>
<style type="text/css">
<!--
.STYLE1 {
font-size: 12px;
color: #FFFFFF;
font-weight: bold;
}
.style2 {font-weight: bold; font-size: 12px;}
-->
</style>
</head>
<body>
<form name="form" method="post" action="">
<table width="1003" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="168" height="169" background="images/index_01.gif"> </td>
<td width="685" background="images/index_02.gif"> </td>
<td width="150" background="images/index_03.gif"> </td>
</tr>
<tr>
<td width="168" height="311" background="images/index_04.gif"> </td>
<td background="images/index_05.gif"><table width="675" height="169" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="43" align="center" valign="baseline"> </td>
<td align="center" valign="middle"> </td>
<td align="center" valign="baseline"> </td>
</tr>
<tr>
<td width="382" height="24" align="center" valign="baseline"> </td>
<td width="207" height="24" valign="middle"><span class="style2">用戶名</span><span class="STYLE1">
<input name="txt_user" id="txt_user" style="height:20px " size="10">
</span></td>
<td width="86" height="24" align="center" valign="baseline"> </td>
</tr>
<tr>
<td height="24" align="center" valign="baseline"> </td>
<td height="24" valign="middle"><span class="style2">密碼</span><span class="STYLE1">
<input name="txt_pwd" type="password" id="txt_pwd" style="FONT-SIZE: 9pt; height:20px" size="10">
</span></td>
<td height="24" align="center" valign="baseline"> </td>
</tr>
<tr>
<td height="24" align="center" valign="baseline"> </td>
<td height="24" valign="middle"><span class="style2">驗證碼</span><span class="STYLE1">
<input name="checks" size="6" style="height:20px ">
<img src="checks.php" width="70" height="18" border="0" align="bottom"></span> </td>
<td height="24" align="center" valign="baseline"> </td>
</tr>
<tr>
<td height="40" align="center" valign="baseline"> </td>
<td align="center" valign="baseline"> <input type="submit" name="Submit" value="登錄"></td>
<td align="center" valign="baseline"> </td>
</tr>
</table></td>
<td background="images/index_06.gif"> </td>
</tr>
<tr>
<td height="100"> </td>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
使用柱形圖
<?php
include ("jpgraph/jpgraph.php");
include ("jpgraph/jpgraph_bar.php");
$datay=array(160,180,203,289,405,488,489,408,299,166,187,105);
//創建畫布
$graph = new Graph(600,300,"auto");
$graph->SetScale("textlin");
$graph->yaxis->scale->SetGrace(20);
//創建畫布陰影
$graph->SetShadow();
//設置顯示區左、右、上、下距邊線的距離,單位為像素
$graph->img->SetMargin(40,30,30,40);
//創建一個矩形的對象
$bplot = new BarPlot($datay);
//設置柱形圖的顏色
$bplot->SetFillColor('orange');
//設置顯示數字
$bplot->value->Show();
//在柱形圖中顯示格式化的圖書銷量
$bplot->value->SetFormat('%d');
//將柱形圖添加到圖像中
$graph->Add($bplot);
//設置畫布背景色為淡藍色
$graph->SetMarginColor("lightblue");
//創建標題
$graph->title->Set("《PHP》");
//設置X坐標軸文字
$a=array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");
$graph->xaxis->SetTickLabels($a);
//設置字體
$graph->title->SetFont(FF_SIMSUN);
$graph->xaxis->SetFont(FF_SIMSUN);
//輸出矩形圖表
$graph->Stroke();
?>
使用折線圖統計
<?php
include ("jpgraph/jpgraph.php");
include ("jpgraph/jpgraph_line.php"); //引用折線圖LinePlot類文件
$datay = array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460); //填充的數據
$graph = new Graph(600,300,"auto"); //創建畫布
$graph->img->SetMargin(50,40,30,40); //設置統計圖所在畫布的位置,左邊距50、右邊距40、上邊距30、下邊距40,單位為像素
$graph->img->SetAntiAliasing(); //設置折線的平滑狀態
$graph->SetScale("textlin"); //設置刻度樣式
$graph->SetShadow(); //創建畫布陰影
$graph->title->Set("2000年PHP圖書月銷售額折線圖"); //設置標題
$graph->title->SetFont(FF_SIMSUN,FS_BOLD); //設置標題字體
$graph->SetMarginColor("lightblue"); //設置畫布的背景顏色為淡藍色
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //設置Y軸標題的字體
$graph->xaxis->SetPos("min");
$graph->yaxis->HideZeroLabel();
$graph->ygrid->SetFill(true,'#[email protected]','#[email protected]');
$a=array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"); //X軸
$graph->xaxis->SetTickLabels($a); //設置X軸
$graph->xaxis->SetFont(FF_SIMSUN); //設置X坐標軸的字體
$graph->yscale->SetGrace(20);
$p1 = new LinePlot($datay); //創建折線圖對象
$p1->mark->SetType(MARK_FILLEDCIRCLE); //設置數據坐標點為圓形標記
$p1->mark->SetFillColor("red"); //設置填充的顏色
$p1->mark->SetWidth(4); //設置圓形標記的直徑為4像素
$p1->SetColor("blue"); //設置折形顏色為藍色
$p1->SetCenter(); //在X軸的各坐標點中心位置繪製折線
$graph->Add($p1); //在統計圖上繪製折線
$graph->Stroke(); //輸出圖像
?>
應用3D餅形圖
<?php
include_once ("jpgraph/jpgraph.php");
include_once ("jpgraph/jpgraph_pie.php");
include_once ("jpgraph/jpgraph_pie3d.php"); //引用3D餅圖PiePlot3D對象所在的類文件
$data = array(266036,295621,335851,254256,254254,685425); //定義數組
$graph = new PieGraph(540,260,'auto'); //創建畫布
$graph->SetShadow(); //設置畫布陰影
$graph->title->Set("應用3D餅形圖統計2000年商品的年銷售額比率"); //創建標題
$graph->title->SetFont(FF_SIMSUN,FS_BOLD); //設置標題字體
$graph->legend->SetFont(FF_SIMSUN,FS_NORMAL); //設置圖例字體
$p1 = new PiePlot3D($data); //創建3D餅形圖對象
$p1->SetLegends(array("IT數位","家電通訊","家居日用","服裝鞋帽","健康美容","食品煙酒"));
$targ=array("pie3d_csimex1.php?v=1","pie3d_csimex1.php?v=2","pie3d_csimex1.php?v=3",
"pie3d_csimex1.php?v=4","pie3d_csimex1.php?v=5","pie3d_csimex1.php?v=6");
$alts=array("val=%d","val=%d","val=%d","val=%d","val=%d","val=%d");
$p1->SetCSIMTargets($targ,$alts);
$p1->SetCenter(0.4,0.5); //設置餅形圖所在畫布的位置
$graph->Add($p1); //將3D餅圖形添加到圖像中
$graph->StrokeCSIM(); //輸出圖像到瀏覽器
?>
應用柱形圖依次統計2000年月銷量
<?php
include ("jpgraph/jpgraph.php");
include ("jpgraph/jpgraph_bar.php");
$datay1=array(58,85,65,39,120,91,152,49,97,130,67);
$datay2=array(18,35,101,69,138,131,112,149,88,60,77);
$graph = new Graph(620,300,'auto');
$graph->SetScale("textlin");
$graph->SetShadow();
$graph->SetMarginColor("yellow"); //設置畫布背景色為淡藍色
$graph->img->SetMargin(40,30,40,40);
$graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());
$graph->xaxis->title->Set('');
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->title->Set('應用柱形圖依次統計2000年月銷量');
$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
$bplot1 = new BarPlot($datay1);
$bplot2 = new BarPlot($datay2);
$bplot1->SetFillColor("orange");
$bplot2->SetFillColor("lightblue");
$bplot1->SetShadow();
$bplot2->SetShadow();
$bplot1->SetShadow();
$bplot2->SetShadow();
$gbarplot = new GroupBarPlot(array($bplot1,$bplot2));
$gbarplot->SetWidth(0.6);
$graph->Add($gbarplot);
$graph->Stroke();
?>
轎車的月銷量統計
<?php
include ("jpgraph/jpgraph.php");
include ("jpgraph/jpgraph_line.php");
include ("jpgraph/jpgraph_scatter.php");
$datay1 = array(83,57,93,112,142,112,89,125,69,105,118,75); //定義數組
//創建畫布
$graph = new Graph(620,260);
$graph->SetMarginColor('red');
$graph->SetScale("textlin");
$graph->SetFrame(false);
$graph->SetMargin(30,5,25,20);
//創建標簽標題、顏色、文字大小等屬性
$graph->tabtitle->Set(' 2000年轎車的月銷量統計 ' );
$graph->tabtitle->SetFont(FF_SIMSUN,FS_NORMAL,10);
$graph->tabtitle->SetColor('darkred','yellow');
// 設置X軸網格
$graph->xgrid->Show();
//應用月份做為X軸的坐標
$graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());
//創建折線對象
$p1 = new LinePlot($datay1);
$p1->SetColor("navy");
$p1->mark->SetType(MARK_IMG,'car.gif',0.8); //載入汽車模型標記,並限制其輸出大小
//輸出汽車模型標記的位置
$p1->value->SetFormat('%d');
$p1->value->Show();
$p1->value->SetColor('darkred');
$p1->value->SetFont(FF_ARIAL,FS_BOLD,10);
$p1->value->SetMargin(14); //設置汽車銷量距汽車模型標記的距離
$p1->SetCenter(); //設置汽車銷量及模型在X軸各坐標點居中顯示
$graph->Add($p1); //添加折線圖到圖像中
$graph->Stroke(); //輸出圖像到瀏覽器
?>
統計2006年、2007年、2008年、2009年農產品的產量比率
<?php
include ("jpgraph/jpgraph.php");
include ("jpgraph/jpgraph_pie.php");
//定義數組
$data1 = array(40,21,17,14,23);
$data2 = array(60,54,107,24,83);
$data3 = array(52,151,99,110,223);
$data4 = array(70,181,117,114,33);
//創建畫布
$graph = new PieGraph(600,350,"auto");
$graph->SetShadow();
//設置標題名稱
$graph->title->Set("統計2006年、2007年、2008年、2009年農產品的產量比率");
$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->legend->SetFont(FF_SIMSUN,FS_NORMAL);
//創建餅形圖對象
$size=0.13;
$p1 = new PiePlot($data1);
$p1->SetLegends(array("大豆","玉米","水稻","小麥","高梁"));
$p1->SetSize($size);
$p1->SetCenter(0.25,0.32);
$p1->value->SetFont(FF_FONT0);
$p1->title->Set("2006年");
$p1->title->SetFont(FF_SIMSUN,FS_BOLD);
$p2 = new PiePlot($data2);
$p2->SetSize($size);
$p2->SetCenter(0.65,0.32);
$p2->value->SetFont(FF_FONT0);
$p2->title->Set("2007年");
$p2->title->SetFont(FF_SIMSUN,FS_BOLD);
$p3 = new PiePlot($data3);
$p3->SetSize($size);
$p3->SetCenter(0.25,0.75);
$p3->value->SetFont(FF_FONT0);
$p3->title->Set("2008年");
$p3->title->SetFont(FF_SIMSUN,FS_BOLD);
$p4 = new PiePlot($data4);
$p4->SetSize($size);
$p4->SetCenter(0.65,0.75);
$p4->value->SetFont(FF_FONT0);
$p4->title->Set("2009年");
$p4->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->Add($p1);
$graph->Add($p2);
$graph->Add($p3);
$graph->Add($p4);
$graph->Stroke();
?>
文件系統
文件是用來存儲數據的方式之一。
打開文件,關閉文件,讀寫文件,操作文件。
打開文件
resource fopen ( string filename, string mode [, bool use_include_path]);
關閉文件
bool fclose ( resource handle ) ;
<?php
$f_open =fopen("../file.txt.","rb"); //打開文件
… //對文件進行操作
fclose($f_open) //操作完成後關閉文件
?>
讀取數據
可以讀取一個字元,一行字串,整個文件
讀取文件:readfile()、file()和file_get_contents()
readfile()函數
讀入一個文件並將其寫入到輸出緩衝
int readfile(string filename)
file()函數讀取整個文件的內容
file()函數將文件內容按行存放到數組中
array file(string filename)
file_get_contents()函數
將文件內容(filename)讀入一個字元串
string file_get_contents(string filename[,int offset[,int maxlen]])
案例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>讀取整個文件</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="250" height="25" align="right" valign="middle" scope="col">使用readfile()函數讀取文件內容:</td>
<td height="25" align="center" valign="middle" scope="col">
<?php readfile('tm.txt'); ?> </td>
</tr>
<tr>
<td height="25" align="right" valign="middle">使用file()函數讀取文件內容:</td>
<td height="25" align="center" valign="middle">
<?php
$f_arr = file('tm.txt');
foreach($f_arr as $cont){
echo $cont."<br>";
}
?></td>
</tr>
<tr>
<td width="250" height="25" align="right" valign="middle" scope="col">使用file_get_contents()函數讀取文件內容:</td>
<td height="25" align="center" valign="middle" scope="col">
<?php
$f_chr = file_get_contents('tm.txt');
echo $f_chr;
?></td>
</tr>
</table>
</body>
</html>
讀取一行數據:fgets()和fgetss()
fgets()函數用於一次讀取一行數據
string fgets( int handle [, int length] )
fgetss()函數是fgets()函數的變體,用於讀取一行數據
string fgetss ( resource handle [, int length [, string
allowable_tags]] )
fgets和fgetss的區別
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fgets和fgetss的區別</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td height="30" align="right" valign="middle" scope="col">使用fgets函數:</td>
<td height="30" align="center" valign="middle" scope="col">
<?php
$fopen = fopen('fun.php','rb');
while(!feof($fopen)){
echo fgets($fopen);
}
fclose($fopen);
?> </td>
</tr>
<tr>
<td height="30" align="right" valign="middle">使用fgetss函數:</td>
<td height="30" align="center" valign="middle">
<?php
$fopen = fopen('fun.php','rb');
while(!feof($fopen)){
echo fgetss($fopen);
}
fclose($fopen);
?> </td>
</tr>
</table>
</body>
</html>
讀取一個字元:fgetc() 讀取任意長度的字串:fread()
string fgetc ( resource handle )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>使用fgetc函數讀取字元</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<pre>
<?php
$fopen = fopen('03.txt','rb');
while(false !== ($chr = fgetc($fopen))){
echo $chr;
}
fclose($fopen);
?>
</pre>
</body>
</html>
string fread ( int handle, int length )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>使用fread函數讀取文件</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
$filename = "04.txt";
$fp = fopen($filename,"rb");
echo fread($fp,32);
echo "<p>";
echo fread($fp,filesize($filename));
?>
</body>
</html>
寫入文件
使用fwrite()和file_put_contents()函數向文件中寫入數據
fwrite()函數
int fwrite ( resource handle, string string [, int length] )
file_put_contents()函數
int file_put_contents ( string filename, string data [, int
flags])
使用fwrite和file_put_contents函數寫入數據
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>使用fwrite和file_put_contents函數寫入數據</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
$filepath = "05.txt";
$str = "此情可待成追憶 只是當時已惘然<br>";
echo "用fwrite寫入文件:";
$fopen = fopen($filepath,'wb') or die('文件不存在');
fwrite($fopen,$str);
fclose($fopen);
readfile($filepath);
echo "<p>用file_put_contents寫入文件:";
file_put_contents($filepath,$str);
readfile($filepath);
?>
</body>
</html>
操作文件
bool copy();
bool rename();
bool unlink();
int fileatime();
int filemtime();
int filesize();
array pathinfo();
string realpath();
目 錄 處 理
打開/關閉目錄
瀏覽目錄
操作目錄
opendir()函數
resource opendir ( string path)
closedir()函數
void closedir ( resource handle )
案例:
<?php
$path = "D:\\55555\\www\\55\\55\\12" ;
if (is_dir($path)){ //檢測是否是一個目錄
if ($dire = opendir($path)) //判斷打開目錄是否成功
echo $dire; //輸出目錄指針
}else{
echo '路徑錯誤';
exit();
}
… //其他操作
closedir($dire); //關閉目錄
?>
瀏覽目錄,scandir()函數
array scandir ( string directory [, int sorting_order ])
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>瀏覽目錄</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
$path = 'E:\AppServ\www\mr\sl\13';
if(is_dir($path)){
$dir = scandir($path);
foreach($dir as $value){
echo $value."<br>";
}
}else{
echo "目錄路徑錯誤!";
}
?>
</body>
</html>
操作目錄
bool mkdir
bool rmdir
string getcwd
bool chdir
float disk_free_space
string readdir
void rewinddir
高級應用
遠程文件訪問
文件指針
鎖定文件
php.ini中配置,找到allow_url_fopen,設為ON
fopen('http://127.0.0.1/index.php','rb');
文件指針
1.rewind()函數
bool rewind ( resource handle )
2.fseek()函數
int fseek ( resource handle, int offset [, int whence] )
handle參數為要打開的文件
offset為指針位置或相對whence參數的偏移量,可以是負值
whence的值包括3種:
SEEK_SET,位置等於offset位元組
SEEK_CUR,位置等於當前位置加上offset位元組
SEEK_END,位置等於文件尾加上offset位元組
如果忽略whence參數,系統預設為SEEK_SET
3.feof()函數
bool feof ( resource handle )
4.ftell()函數
int ftell ( resource handle )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文件指針函數</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
$filename = "07.txt";
$total = filesize($filename);
if(is_file($filename)){
echo "文件總位元組數:".$total."<br>";
$fopen = fopen($filename,'rb');
echo "初始指針位置是:".ftell($fopen)."<br>";
fseek($fopen,33);
echo "使用fseek()函數後指針位置:".ftell($fopen)."<br>";
echo "輸出當前指針後面的內容:".fgets($fopen)."<br>";
if(feof($fopen))
echo "當前指針指向文件末尾:".ftell($fopen)."<br>";
rewind($fopen);
echo "使用rewind()函數後指針的位置:".ftell($fopen)."<br>";
echo "輸出前33位元組的內容:".fgets($fopen,33);
fclose($fopen);
}else{
echo "文件不存在";
}
?>
</body>
</html>
鎖定文件
bool flock ( int handle, int operation)
LOCK_SH取得共用鎖定(讀取程式)
LOCK_EX取得獨占鎖定(寫入程式)
LOCK_UN釋放鎖定
LOCK_NB防止flock()在鎖定時堵塞
operation的參數
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>使用flock函數</title>
</head>
<body>
<?php
$filename = '08.txt'; //聲明要打開的文件的名稱
$fd = fopen($filename,'w'); //以w形式打開文件
flock($fd, LOCK_EX); //鎖定文件(毒針共用
fwrite($fd, "hightman1"); //向文件中寫入數據
flock($fd, LOCK_UN); //解除鎖定
fclose($fd); //關閉文件指針
readfile($filename); //輸出文件內容
?>
</body>
</html>
文件上傳
文件上傳是要通過http協議來實現的,要在php.ini文件中進行對上傳文件的設置,要瞭解$_FILES變數和函數move_uploaded_file()函數實現上傳。
要配置php.ini文件,在文件中找到file_uploads,如果值為on,說明伺服器支持文件上傳,如果為off,則表示不支持,upload_tmp_dir為上傳文件臨時位置,系統預設,也可以自己定。
upload_max_filesize為伺服器允許上傳的文件的最大值,預設2mb。max_execution_timePHP中一個指令所能執行的最大時間,memory_limitPHP中一個指令所分配的記憶體空間。
$_FILES[filename][name]
上傳文件的文件名
$_FILES[filename][size]
文件大小
$_FILES[filename][tmp_name]
為臨時文件名
$_FILES[filename][type]
文件的類型
$_FILES[filename][error]
上傳文件的結果。如果返回0,說明文件上傳成功
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>$_FIELS變數</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<form action="" method="post" enctype="multipart/form-data">
<tr>
<td width="150" height="30" align="right" valign="middle">請選擇上傳文件:</td>
<td width="250"><input type="file" name="upfile"/></td>
<td width="100"><input type="submit" name="submit" value="上傳" /></td>
</tr>
</form>
</table>
<?php
if(!empty($_FILES)){
foreach($_FILES['upfile'] as $name => $value)
echo $name.' = '.$value.'<br>';
}
?>
</body>
</html>
文件上傳函數
move_uploaded_file()函數上傳文件
bool move_uploaded_file ( string filename, string destination )
將上傳文件存儲到指定的位置
單文件上傳
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>單文件上傳</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
if(!empty($_FILES[up_file][name])){
$fileinfo = $_FILES[up_file];
if($fileinfo['size'] < 1000000 && $fileinfo['size'] > 0){
move_uploaded_file($fileinfo['tmp_name'],$fileinfo['name']);
echo '上傳成功';
}else{
echo '文件太大或未知';
}
}
?>
<table width="385" height="185" border="0" cellpadding="0" cellspacing="0" background="images/bg.JPG">
<tr>
<td width="142" height="80"> </td>
<td width="174"> </td>
<td width="69"> </td>
</tr>
<form action="" method="post" enctype="multipart/form-data" name="form">
<tr>
<td height="30"> </td>
<td align="left" valign="middle"><input name="up_file" type="file" size="12" /></td>
<td> </td>
</tr>
<tr>
<td height="27" align="right"> </td>
<td align="center" valign="top"> <input type="image" name="imageField" src="images/fg.bmp"></td>
<td> </td>
</tr>
</form>
<tr>
<td height="48"> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>
多文件上傳
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>多文件上傳</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style>
</head>
<body>
請選擇要上傳的文件
<form action="" method="post" enctype="multipart/form-data">
<table border="1" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#CCCCCC" id="up_table" >
<tbody id="auto">
<tr id="show" >
<td bgcolor="#FFFFFF">上傳文件 </td>
<td bgcolor="#FFFFFF"><input name="u_file[]" type="file"></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">上傳文件 </td>
<td bgcolor="#FFFFFF"><input name="u_file[]" type="file"></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">上傳文件 </td>
<td bgcolor="#FFFFFF"><input name="u_file[]" type="file"></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">上傳文件 </td>
<td bgcolor="#FFFFFF"><input name="u_file[]" type="file"></td>
</tr>
</tbody>
<tr>
<td colspan="4" bgcolor="#FFFFFF"><input type="submit" value="上傳" /></td>
</tr>
</table>
</form>
<?php
if(!empty($_FILES[u_file][name])){
$file_name = $_FILES[u_file][name];
$file_tmp_name = $_FILES[u_file][tmp_name];
for($i = 0; $i < count($file_name); $i++){
if($file_name[$i] != ''){
move_uploaded_file($file_tmp_name[$i],$i.$file_name[$i]);
echo '文件'.$file_name[$i].'上傳成功。更名為'.$i.$file_name[$i].'<br>';
}
}
}
?>
</body>
</html>
訪問量:
<?php session_start();
if($_SESSION[temp]==""){ //判斷$_SESSION[temp]==""的值是否為空,其中的temp為自定義的變數
if(($fp=fopen("counter.txt","r"))==false){
echo "打開文件失敗!";
}else{
$counter=fgets($fp,1024); //讀取文件中數據
fclose($fp); //關閉文本文件
$counter++; //計數器增加1
$fp=fopen("counter.txt","w"); //以寫的方式打開文本文件<!---->
fputs($fp,$counter); //將新的統計數據增加1
fclose($fp);
} //關閉文
$_SESSION[temp]=1; //登錄以後,$_SESSION[temp]的值不為空,給$_SESSION[temp]賦一個值1
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>通過文本文件統計頁面訪問量</title>
</head>
<body>
<img src="gd1.php" />
</BODY>
</HTML>
<?php
//以圖形的形式輸出資料庫中的記錄數
if(($fp=fopen("counter.txt","r"))==false){
echo "打開文件失敗!";
}else{
$counter=fgets($fp,1024);
fclose($fp);
//通過GD2函數創建畫布
$im=imagecreate(240,24);
$gray=imagecolorallocate($im,255,255,255);
$color =imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); //定義字體顏色
//輸出中文字元
$text=iconv("gb2312","utf-8","網站的訪問量:"); //對指定的中文字元串進行轉換
$font = "Fonts/FZHCJW.TTF";
imagettftext($im,14,0,20,18,$color,$font,$text); //輸出中文
//輸出網站的訪問次數
imagestring($im,5,160,5,$counter,$color);
imagepng($im);
imagedestroy($im);
}
?>
限制大小的文件上傳
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>限制大小的文件上傳</title>
<style type="text/css">
<!--
body {
margin-left: 00px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style></head>
<body>
<table width="385" height="185" border="0" cellpadding="0" cellspacing="0" background="images/bg.JPG">
<tr>
<td width="142" height="80"> </td>
<td width="174"> </td>
<td width="69"> </td>
</tr>
<form name="form1" method="post" action="index_ok.php" enctype="multipart/form-data">
<tr>
<td height="30"> </td>
<td align="left" valign="middle"><input name="files" type="file" id="files" size="13" maxlength="150"> </td>
<td> </td>
</tr>
<tr>
<td height="27" align="right"> </td>
<td align="center" valign="top"> <input type="image" name="imageField" src="images/fg.bmp"></td>
<td> </td>
</tr>
</form>
<tr>
<td height="48"> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>
<?php
if($_FILES['files']['name']==true){
$filesize=$_FILES['files']['size'];
if($filesize>1000000){
echo "對不起,您上傳的文件超過規定的大小!!";
echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php\">將在3秒鐘後返回前頁...";
}else{
$path = './upfiles/'. $_FILES['files']['name'];
if (move_uploaded_file($_FILES['files']['tmp_name'],$path)) {
echo "上傳成功!!";
echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php\">";
}else{
echo "文件上傳失敗!!";
echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php\">";
}
}
}
?>
結言
好了,歡迎在留言區留言,與大家分享你的經驗和心得。
感謝你學習今天的內容,如果你覺得這篇文章對你有幫助的話,也歡迎把它分享給更多的朋友,感謝。
感謝!承蒙關照!您真誠的贊賞是我前進的最大動力!