php之高级技术--与文件系统和服务器的交互
1.文件上传
1)在<form>标记中,必须设置属性enctyp="multipart/form-data"--服务器知道上传文件带有常规的表单信息。
2)必须有个可以设置上传文件最大长度的表单域。hidden
3)需要指定文件类型:file
<form enctype="multipart/form-data" action="upload.php" method="post">
<p><input type="hidden" name="MAX_FILE_SIZE" value="1000000" /></p>
<p>Upload this file: <input type="file" name="userfile" /></p>
<p><input type="submit" name="submit" value="Send File" /></p>
</form>
》当文件被上传时,该文件将保持在web服务器上的临时目录中-web服务器默认的临时目录
如果在脚本执行完成之前不移动,复制或更改文件名,该文件将删除。
PHP中,需要处理的数据保持在超级全局变量$_FILE中:
$_FILES['userfile']['name']---用户系统中文件名。、
$_FILES['userfile']['size]----文件大小
$_FILES['userfile']['type']---文件的MIME类型
$_FILES['userfile']['error']---任何与文件上传有关的错误代码。
1-upload_max_filesize
2-max_file_size
3-only partially uploaded
4-no file uploaded.
//reformat the file contents
$fp = fopen($upload, 'r');
$contents = fread($fp , filesize($upload));
fclose($fp);
$contents = strip_tags($contents);
$fp = fopen($upload, 'w');
fwrite($fp, $contents);
fclose($fp);
//show what was uploaded
echo 'Preview of uploaded file contents:<br /> <hr />';
echo $contents;
上传文件注意问题:
- 身份验证
- 文件的类型,内容,名字验证
- 确保文件夹路径
- 文件名合法字符--系统
- PHP配置文件将upload_tmp_dir指令设置层指向有访问权限的目录。
2.使用目录函数
opendir(),closedir(),readdir();
dir类----handle,path属性 read(),close(),rewind()
获得当前目录的信息: dirname($path) basename($path) disk_free_space($path)
创建和删除目录:mkdir('/tmp/testing', 0777),rmdir()
3.与文件系统的交互:
获取文件信息:
while ($file=readdir($dir)) {
echo '<a href="filedetails.php?file='. $file.'">' . $file .'</a><br>';
文件测试:
is_dir,is_file(),is_link(),is_readable(),is_writeable()
fileperms()
filetype()
与环境变量交互:
getenv('HTTP_REFERER')
putenv('HOME=$home')

浙公网安备 33010602011771号