1,文件
1,讀取文件內容
$content = file_get_contents('./test.txt'); // 將整個文件全部讀取到一個字符串中
$content = file_get_contents('./test.txt', null, null, 100, 500); // 通過參數控制讀取內容的開始點以及長度
/*
* 提供類似於C語言操作文件的方法,使用fopen,fgets,fread等方法,
* fgets可以從文件指針中讀取一行,freads可以讀取指定長度的字符串。
*/
$fp = fopen('./text.txt', 'rb');
while (! feof($fp))
{
echo fgets($fp); // 讀取一行
}
fclose($fp);
$fp = fopen('./text.txt', 'rb');
$contents = '';
while (! feof($fp))
{
$contents .= fread($fp, 4096); // 一次讀取4096個字符
}
fclose($fp);
$filename = './test.txt';
/* ----------------------------對文件,存在與否,進行校驗---------------------------- */
file_exists($filename); // 校驗文件或者文件夾是否存在
is_file($filename); // 校驗一個文件是否存在
is_writeable($filename); // 文件 是否 可寫
is_readable($filename); // 文件 是否 可讀
/* ----------------------------對文件,頭部信息,進行校驗---------------------------- */
fileowner($filename); // 獲得文件的所有者
filectime($filename); // 獲取文件的創建時間
filemtime($filename); // 獲取文件的修改時間
fileatime($filename); // 獲取文件的訪問時間
/* ----------------------------文件大小---------------------------- */
filesize($filename); // 獲取文件大小
/**
* 獲取文件大小
* @param unknown $size
* @param string $format
* @return string
*/
function getsize($size, $format = 'kb')
{
$p = 0;
if ($format == 'kb')
{
$p = 1;
} elseif ($format == 'mb')
{
$p = 2;
} elseif ($format == 'gb')
{
$p = 3;
}
$size /= pow(1024, $p);
return number_format($size, 3);
}
2,時間
1,獲取日期和時間戳
$time = time(); // unit:s 時間戳
//設置默認的時區
date_default_timezone_set('Asia/Shanghai')."\n";
echo date("Y-m-d")."\n";//2014-03-30
// 時間戳 ==>> 日期
echo date("Y-m-d",'1396193923')."\n";//2014-03-30,1396193923表示2014-03-30的unix時間戳
// 日期 ==>> 時間戳
// 1398700800,這個數字表示從1970年1月1日 00:00:00 到2014年4月29號經歷了1398700800秒
echo strtotime('2014-04-29')."\n";
// 1398700801,這個數字表示從1970年1月1日 00:00:00 到2014-04-29 00:00:01時經歷了1398700801秒
echo strtotime('2014-04-29 00:00:01')."\n";
// 獲取時間戳
echo strtotime("now")."\n";//相當於將英文單詞now直接等於現在的日期和時間,並把這個日期時間轉化爲unix時間戳。這個效果跟echo time();一樣。
echo strtotime("+1 seconds")."\n";//相當於將現在的日期和時間加上了1秒,並把這個日期時間轉化爲unix時間戳。這個效果跟echo time()+1;一樣。
echo strtotime("+1 day")."\n";//相當於將現在的日期和時間加上了1天。
echo strtotime("+1 week")."\n";//相當於將現在的日期和時間加上了1周。
echo strtotime("+1 week 3 days 7 hours 5 seconds")."\n";//相當於將現在的日期和時間加上了1周3天7小時5秒。
// 輸出爲:2014-05-01 15:15:22
echo date('Y-m-d H:i:s', time())."\n";
// 輸出爲:2014-05-01 07:15:22
echo gmdate('Y-m-d H:i:s', time())."\n"; // 因爲格林威治時間是現在中國時區的時間減去8個小時,所以相對於現在時間要少8個小時
3,GD圖形圖像
1)簡介
$img = imagecreatetruecolor(100, 100); // 創建一個畫布
$black = imagecolorallocate($img, 0x00, 0x00, 0x00);
$red = imagecolorallocate($img, 0xFF, 0x00, 0x00);
$blue = imagecolorallocate($img, 0x00, 0x00, 0xFF);
$green = imagecolorallocate($img, 0x00, 0xFF, 0x00);
$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
imagefill($img, 0, 0, $red); // 填充背景
imageline($img, 0, 0, 100, 100, $blue); // 繪製線條
imagestring($img, 5, 0, 0, "Hello world", $green); // 繪製字體
// 生成隨機的驗證碼
$code = '';
for ($i = 0; $i < 4; $i ++)
{
$code .= rand(0, 9);
}
imagestring($img, 5, 10, 10, $code, $black);
// 加入噪點干擾
for ($i = 0; $i < 50; $i ++)
{
imagesetpixel($img, rand(0, 100), rand(0, 100), $black);
imagesetpixel($img, rand(0, 100), rand(0, 100), $green);
}
header("content-type:image/png");
/*
* 使用imagejpeg將圖片保存成jpeg格式,imagegif將圖片保存成gif格式,
* 需要說明的是,imagejpeg會對圖片進行壓縮,因此還可以設置一個質量參數。
*/
imagepng($img);
// imagepng($img, 'img.png'); 保存成文件
// imagejpeg($img, 'img.png', 80); 壓縮質量
imagedestroy($img);
// 最大的問題在於:有些網站的圖片,採用這種方式,會被禁掉;對於我這種小白,很難找修改方案
// 準備一些素材圖片
$img_url = 'http://imgstore.cdn.sogou.com/app/a/100540002/667592.jpg';
$img_content = file_get_contents($img_url);
$img_file_name = 'tmp.jpg';
file_put_contents($img_file_name, $img_content);
$logo_url = 'http://wiki.ubuntu.org.cn/images/3/3b/Qref_Edubuntu_Logo.png';
$logo_file_name = 'logo.png';
file_put_contents($logo_file_name, file_get_contents($logo_url));
// 水印操作
$result_img = imagecreatefromjpeg($img_file_name);
$result_logo = imagecreatefrompng($logo_file_name);
$result_logo_size = getimagesize($logo_file_name);
imagecopy($result_img, $result_logo, 15, 15, 0, 0, $result_logo_size[0], $result_logo_size[1]);
header("content-type: image/jpeg");
imagejpeg($result_img);