PHP file_exists() 函数
file_exists() 函数检查文件或目录是否存在。
如果指定的文件或目录存在则返回 true,否则返回 false。
语法
file_exists(path)
参数 | 描述 |
---|---|
path | 必需。规定要检查的路径。 |
例子
<?php echo file_exists("test.txt"); ?>
PHP is_writable() 函数
is_writable() 函数判断指定的文件是否可写。
语法
is_writable(file)
参数 | 描述 |
---|---|
file | 必需。规定要检查的文件。 |
说明
如果文件存在并且可写则返回 true。file 参数可以是一个允许进行是否可写检查的目录名。
提示和注释
注释:本函数的结果会被缓存。请使用 clearstatcache() 来清除缓存。
例子
<?php $file = "test.txt"; if(is_writable($file)) { echo ("$file is writeable"); } else { echo ("$file is not writeable"); } ?>
输出:
test.txt is writeable
PHP str_replace() 函数
定义和用法
str_replace() 函数使用一个字符串替换字符串中的另一些字符。
语法
str_replace(find,replace,string,count)
参数 | 描述 |
---|---|
find | 必需。规定要查找的值。 |
replace | 必需。规定替换 find 中的值的值。 |
string | 必需。规定被搜索的字符串。 |
count | 可选。一个变量,对替换数进行计数。 |
例子
例子 1
<?php
echo str_replace("world","John","Hello world!")
;
?>
输出:
Hello John!
例子 2
在本例中,我们将演示带有数组和 count 变量的 str_replace() 函数:
<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i)
);
echo "Replacements: $i";
?>
输出:
Array ( [0] => blue [1] => pink [2] => green [3] => yellow ) Replacements: 1
例子 3
<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr)
);
?>
输出:
Array ( [0] => B [1] => [2] => ! )
$_FILES 函数
第一个参数是表单的 input name,第二个下标可以是 "name", "type", "size", "tmp_name" 或 "error"。就像这样:
- $_FILES["file"]["name"] - 被上传文件的名称
- $_FILES["file"]["type"] - 被上传文件的类型
- $_FILES["file"]["size"] - 被上传文件的大小,以字节计
- $_FILES["file"]["tmp_name"] - 存储在服务器的文件的临时副本的名称
- $_FILES["file"]["error"] - 由文件上传导致的错误代码
这是一种非常简单文件上传方式。基于安全方面的考虑,您应当增加有关什么用户有权上传文件的限制。
PHP in_array() 函数
in_array() 函数在数组中搜索给定的值。
语法
in_array(value,array,type)
参数 | 描述 |
---|---|
value | 必需。规定要在数组搜索的值。 |
array | 必需。规定要搜索的数组。 |
type | 可选。如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同。 |
说明
如果给定的值 value 存在于数组 array 中则返回 true。如果第三个参数设置为 true,函数只有在元素存在于数组中且数据类型与给定值相同时才返回 true。
如果没有在数组中找到参数,函数返回 false。
注释:如果 value 参数是字符串,且 type 参数设置为 true,则搜索区分大小写。
例子 1
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland"); if (in_array("Glenn",$people)) { echo "Match found"; } else { echo "Match not found"; } ?>
输出:
Match found
例子 2
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland", 23); if (in_array("23",$people, TRUE)) { echo "Match found<br />"; } else { echo "Match not found<br />"; }if (in_array("Glenn",$people, TRUE)) { echo "Match found<br />"; } else { echo "Match not found<br />"; }if (in_array(23,$people, TRUE)) { echo "Match found<br />"; } else { echo "Match not found<br />"; } ?>
输出:
Match not found Match found Match found