1. 程式人生 > >10.31隨筆

10.31隨筆

php 常用的日期函式
1540949469 時間戳 當前距離1970年1月1日 0:0:0 的秒數 返回單位是秒
//echo (time());
//date()日期函式
echo (date("y-m-d h:i:s")); //獲取當前時間 y-m-d 年-月-日 h:i:s 時-分-秒

php 設定預設的時區
date_default_timezone_set("PRC");   //中國(北京)的時區 
//echo (date_default_timezone_get()); //獲取設定的時區 
是在配置檔案設定 php.ini                  date.timezone = PRC

 

 

補充:

幾個陣列函式

array_merge()  合併陣列或者合併多個數組

array_sum  統計所有的總和

array_slice(擷取的陣列,擷取的元素(這裡的元素不是下標))
array_slice (擷取的陣列,起始元素,長度)

strrpos ( ) 1獲取最後出現的位置  2從右向左擷取

 

 

php 檔案函式 
is_writeable("檔案路徑")    判斷檔案是否可寫 //檔案是否存在

如果可寫 返回true 檔案不存在或者只讀  返回false 

 

is_executable("檔案路徑")   判斷檔案是否可以執行 windows .exe  .sys  .com 都是可執行檔案

 

file_exists()   檔案或者目錄是否存在

filesize() 檔案的大小(讀具體檔案的大小) 單位是b 位元組
1G = 1024M
1M = 1024KB
1KB = 1024B

 

 

 

php 檔案建立時間 檔案修改時間 檔案訪問時間
filectime("檔案路徑") create:建立 獲取檔案的建立時間
filemtime("檔案路徑") modify:修改 獲取檔案的修改時間
fileatime("檔案路徑") activor:訪問 獲取檔案的訪問時間

 

opendir(“開啟目錄地址”)  開啟一個目錄
readdir(“讀取的內容”)  讀取目錄中的內容              返回的是個路徑 .  ..   ./  ../

 

 

php檔案上傳 圖片上傳功能 

php用表單上傳                         步驟: 1.上傳位置2.檔案型別3.檔案大小4.檔案重新命名5.檔案儲存到資料庫
用ajax非同步提交                       

(1):表單設定上傳圖片 表單上加入 enctype="multipart/form-data"
(2):$_FILES 超級陣列
Array
(
[imgs] => Array
(
[name] => 6.jpg 圖片名字
[type] => image/jpeg 圖片型別
[tmp_name] => C:\Users\Administrator\AppData\Local\Temp\php75C3.tmp
圖片存放地址(虛擬地址)
[error] => 0 0:上傳成功 1:上傳失敗
[size] => 132244 圖片大小 返回的位元組
)

)
(3):上傳圖片的函式 move_uploaded_file("原有路徑","現有路徑") 現有路徑覆蓋原有路徑 <=> copy()
上傳圖片的函式 copy()

(4):處理圖片命名 

(5):圖片上傳的格式 

(6):控制圖片大小

(7):圖片的地址存入到資料庫

 

例子:

<form method="post" action="10.31.php" enctype="multipart/form-data">
<input type="file" name="imgs" /><br/>
<input type="submit" value="上傳圖片"/>
</form>

<?php
if(!empty($_FILES)){
//判斷檔案大小
if($_FILES['imgs']['size'] > 2*1024*1024)
{
die("你上傳的檔案太大"); 

}
//判斷檔案的型別
$path = array(".jpg",".png",".gif",".bmp");
$pathname = substr($_FILES['imgs']['name'],strrpos($_FILES['imgs']['name'],"."));
if(!in_array($pathname,$path))
{
die("你上傳的檔案型別不符合"); 
}
//檔案重新命名
$file_img = time().rand(99,5000).$pathname;

//建立資料夾
$file = "uploads/".date("y");
if(!file_exists($file))
{
mkdir($file);//建立一個檔案 
}
$file = $file."/".date("m-d");
if(!file_exists($file))
{
mkdir($file);//建立一個檔案 
}
$path = $file."/".$file_img;
//上傳圖片 
copy($_FILES['imgs']['tmp_name'],$path);
}
?>

 

 

全靜態快取     偽靜態快取

$b = file_get_contents(“獲取檔案資訊”)
file_put_contents("讀取到自己檔案裡面",$b)

載入檔案

include("xx.html"); //載入檢視檔案

 

圖片繪畫  驗證碼

先開啟gd庫

application programme interface   interface:介面  application:應用程式

 

header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate"); 設定圖片繪畫(圖片大小 )

header("Content-type: image/png;charset=utf8");設定圖片的型別,和字符集

$im = imagecreatetruecolor(寬度, 高度);返回影象資源 

$back_color = imagecolorallocate($im, 紅, 綠, 藍);使用三元色畫背景

imagefilledrectangle(影象資源,右上x,右上y,左下x,左下y,顏色) //畫矩形

imagerectangle($im, 左上x, 左上y, 右下x, 右下y,顏色資源); //畫邊框 

imagesetpixel($im, x座標,y座標,顏色資源) //畫干擾點

 imageline影象資源,右上x,右上y,左下x,左下y,顏色) //畫直線

imagearc($im,中點x,中點y,橫距離,豎距離,起始角度,結束角度,顏色) //畫曲線

imagepng($im);//生成影象

imagedestroy($im);//銷燬圖片

imagefttext()  能繪製UTF-8編碼的字串漢字

 

小例子:

<?php
header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
header("Content-type: image/png;charset=utf8");
//指定邊框的寬度和高度
$s_width = 100;
$h_height= 80;
$im = imagecreatetruecolor($s_width, $h_height);
//畫矩形(邊框)
$back_color = imagecolorallocate($im,255,255,255); //使用三元色畫背景
imagefilledrectangle($im,0,0,$s_width,$h_height,$back_color);//畫框

//畫干擾點
for($i=0;$i<20;$i++)
{
$tpixe_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
//imagesetpixel($im, x座標,y座標,顏色資源)
imagesetpixel($im,rand(0,$s_width),rand(0,$h_height),$tpixe_color);
}

//畫直線
for($i=0;$i<10;$i++)
{
$line_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); 
imageline($im,rand(0,$s_width),rand(0,$h_height),rand(0,$s_width),rand(0,$h_height),$line_color );
}

//畫弧線
for($i=0;$i<10;$i++)
{
//imagearc($im,中點x,中點y,橫距離,豎距離,起始角度,結束角度,顏色)

$arc_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); 
imagearc($im,rand(0,$s_width),rand(0,$h_height),rand(0,100),rand(0,100),rand(-90,90),rand(70,360),$arc_color);
}

//畫數字影象
$str = "23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"; 
$x =20;
$y =40; 
for($i=0;$i<4;$i++)
{
//隨機數字
$char = $str[rand(0,strlen($str)-1)];
$font_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); 
imagefttext($im, 20 , rand(-45,45), $x + $i*20, $y, $font_color, "ariblk.ttf", $char);
}

//生成影象
imagepng($im);

//銷燬圖片
imagedestroy($im);//通常與生成圖片連用
?>

 

 

 

 

補充幾道練習題: