1.印副檔名
filename=abc.txt
$a=$_FILES['xxx']['name']
end(explode('.',$a))
end意思是取出陣列最後一個元素
(($_FILES["file"]["type"] == "image/gif")
2.保存檔案
上面的实例在服务器的 PHP 临时文件夹(xammp/temp中创建了一个被上传文件的临时副本。
这个临时的副本文件会在脚本结束时消失。要保存被上传的文件,我们需要把它拷贝到另外的位置:
3. file_exists 查看是否檔存在
4.move_uploaded_file
把檔案從暫時存放區移動到你想要的位置
move_uploaded_file($_FILES['file']['tmp_name'],'./xxx')
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">a</button>
</form>
5.設置cookie
setcookie("user", "Alex Porter", time()+3600);
6.取cookie
echo $_COOKIE["user"];
7.刪cookie 使其變過去時間點
setcookie("user", "", time()-3600);
8.exception
<?php
function a($a){
if($a>0){
throw new Exception("fuck");
}
}
try{
a(5);
}catch(Exception $b){
echo "Message:".$b->getMessage();
}
9.filter_var 過濾器
echo filter_var($a,FILTER_VALIDATE_INT)?'t': 'f';
$a='5' 或5會印t
9.1filter_has_var(INPUT_GET,'email')
是否有email的get變數
9.2filter_input
echo filter_input(INPUT_GET,'email',FILTER_VALIDATE_EMAIL);
filter_input(INPUT_GET, "url", FILTER_SANITIZE_URL)
filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6
假如输入变量是一个类似这样的字符串:"http://www.W3CååSchøøool.cc/",则净化后的 $url 变量如下所示:
{
return str_replace("_", " ", $string);
}
$string = "Peter_is_a_great_guy!";
echo filter_var($string, FILTER_CALLBACK,
array("options"=>"convertSpace"));
10.json_encode 將array或class 物件轉成object
class c{
public $a=5;
public $b=4;
private $c=5;
}
$a=['a'=>1];
$c=new c();
echo json_encode($c);
11.json_decode 將object轉成array 或物件
$a='{"a":1}'
mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])
var_dump(json_decode($a));
var_dump(json_decode($a,true));
assoc=true表示轉成數組false為物件