Abstract: PHP中文件和文件夹操作常用函数以及文件上传功能的介绍和应用,对新闻界面添加用户登录注册界面等。
一、文件和文件夹操作常用函数
1. 文件内容操作
| 函数名 |
作用 |
| fopen() |
打开文件或URL |
| fread() |
读取打开的文件 |
| fgets() |
从打开的文件中返回一行 |
| fputs()/fwrite() |
写入打开的文件 |
| copy() |
复制文件 |
| fclose() |
关闭打开的文件 |
| file_get_contents() |
把文件读入字符串 |
| file_put_contents() |
把字符串写文件 |
| basename() |
返回路径中文件名部分 |
| filesize() |
返回文件大小 |
- fread(handle, length)/filesize(filesize)/copy(file, to_file)
1 2 3 4 5 6 7
| <?php $file = fopen("test.txt", "r"); $contents = fread($file, "10"); $size = filesize("test.txt"); echo copy("source.txt", "target.txt"); fclose($file); ?>
|
1 2 3 4 5 6 7
| <?php $path = "/testweb/home.php"; //home.php echo basename($path) . "<br>"; //home echo basename($path, ".php"); ?>
|
- file_get_contents(path, include_path, context, start, max_length)
1 2 3 4 5
| <?php //This is a test file with test text echo file_get_contents("test.txt");
?>
|
- file_put_contents()函数:成功返回写入文件的字符数,失败则返回False
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?php // 6 echo file_put_contents("sites.txt", "Runoob");
$file = 'sites.txt'; $site = "\nGoogle"; // 向文件追加写入内容 // 使用 FILE_APPEND 标记,可以在文件末尾追加内容 // LOCK_EX 标记可以防止多人同时写入 file_put_contents($file, $site, FILE_APPEND | LOCK_EX);
?>
|
2.文件操作
| 函数名 |
作用 |
| file_exists() |
检查文件或目录是否存在 |
| filetype() |
返回文件类型 |
| is_dir() |
判断文件是否是一个目录 |
| is_file() |
判断文件是否是常规的文件 |
| mkdir() |
创建目录 |
| rename() |
重命名文件或目录 |
| rmdir() |
删除空的目录 |
| unlink() |
删除文件 |
1 2 3 4
| <?php // 1 echo file_exists("test.txt") ?>
|
1 2 3 4 5 6 7
| <?php // fifo char dir block link file unknown //file echo filetype("test.txt"); //dir echo filetype("images"); ?>
|
二、文件上传
1. 创建一个文件上传表单
1 2 3 4 5 6 7 8 9 10 11 12 13
| <html> <head> <meta charset="utf-8"> <title>文件上传</title> </head> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">文件名:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="提交"> </form> </body> </html>
|
<form>标签的enctype属性规定了在提交表单时要使用哪种内容类型。在表单需要二进制数据时,比如文件内容,使用multipart/form-data。
<input>标签type="file"属性规定了应该把输入作为文件来处理。
2. 创建上传脚本 upload_file.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?php if($_FILES["file"]["error"]>0){ //由文件上传导致的错误代码 echo "错误" .$_FILES["file"]["error"] . "<br>"; }else{ //上传文件的名称 echo "上传文件名:" . $_FILES["file"]["name"] . "<br>"; //上传文件的类型 echo "文件类型:" . $_FILES["file"]["type"] . "<br>"; //上传文件的大小 echo "文件大小:" . ($_FILES["file"]["size"]/1024) . "<br>"; //存储在服务器的文件临时副本的名称 echo "文件临时存储位置:" . $_FILES["file"]["tmp_name"]; } ?>
|
3. 上传限制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <?php //允许上传图片后缀 $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES['file']['name']); //获取文件后缀名 $extension = end($temp); if((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 204800) && (in_array($extension, $allowedExts))){ if($_FILES["file"]["error"] > 0){ echo "错误:" . $_FILES["file"]["error"] . "<br>"; }else{ //上传文件的名称 echo "上传文件名:" . $_FILES["file"]["name"] . "<br>"; //上传文件的类型 echo "文件类型:" . $_FILES["file"]["type"] . "<br>"; //上传文件的大小 echo "文件大小:" . ($_FILES["file"]["size"]/1024) . "<br>"; //存储在服务器的文件临时副本的名称 echo "文件临时存储位置:" . $_FILES["file"]["tmp_name"]; } }else{ echo "非法文件的格式"; } ?>
|
4. 保存被上传的文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <?php //允许上传图片后缀 $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES['file']['name']); //获取文件后缀名 $extension = end($temp); if((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 204800) && (in_array($extension, $allowedExts))){ if($_FILES["file"]["error"] > 0){ echo "错误:" . $_FILES["file"]["error"] . "<br>"; }else{ //上传文件的名称 echo "上传文件名:" . $_FILES["file"]["name"] . "<br>"; //上传文件的类型 echo "文件类型:" . $_FILES["file"]["type"] . "<br>"; //上传文件的大小 echo "文件大小:" . ($_FILES["file"]["size"]/1024) . "<br>"; //存储在服务器的文件临时副本的名称 echo "文件临时存储位置:" . $_FILES["file"]["tmp_name"]; //判断当前目录下的upload目录是否存在该文件 //如果没有upload目录,需要创建它,upload目录权限为777 if(file_exists("upload/" . $_FILES["file"]["name"])){ echo $_FILES["file"]["name"] . "文件已经存在"; }else{ //如果upload目录不存在该文件则将文件上传到upload目录下 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "文件存储在:" . "upload/" . $_FILES["file"]["name"]; } } }else{ echo "非法文件的格式"; } ?>
|
关于上传文件的起名常常加上时间戳。