2、文件上传过程
(1)上传文件提交表单html代码:
<!--向服务器上传文件的HTML表单(限制为文本文件)--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Adminstration - upoload new files</title> </head> <body> <h1>Upload new files</h1> <form action="upload.php" method="post" enctyple="multipart/form-data" > <!--enctyple:规定在发送到服务器之前对表单数据进行编码的方式(在上传控件时必须按照以上方式设置该属性)--> <div> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> <!--规定传输文件的最大字节数--> <label for="userfile">Upload a file</label> <!--在<label>内点击文本,会触发该控件,此时浏览器会自动对焦到标签for属性所指向的表单控件上面--> <input type="file" name="userfile" id="userfile"> <!--id属性为<label>标签for所指向控件元素的id号--> <input type="submit" value="Send File"> </div> </form> </body> </html>(2)php处理上传文件代码
<?php //检验文件传输异常 if($_FILES["userfile"]["error"]>0){ echo "Problem"; switch($_FILES["userfile"]["error"]){ case 1: echo "File exceeded upload_max_filesize";break; case 2: echo "File exceeded max_file_size";break; case 3: echo "File only partially upload";break; case 4: echo "No file uploaded";break; case 6: echo "Cannot upload file: No temp directory specified";break; case 7: echo "Upload failed:Cannot write to disk";break; } exit; } //检验传输的文件是否为文本文件 if($_FILES["userfile"]["type"] != "text/plain"){ echo "Problem: file is not plain text"; exit; } //将上传文件包含在服务器中/uploads/的目录下(该目录必须独立于Web文档树) $upfile = "/uploads/".$_FILES["userfile"]["name"]; //在指定目录下以传输文件的文件名创建一个新文件 if(is_uploaded_file($_FILES["userfile"]["tmp_name"])){ if(! move_uploaded_file($_FILES["userfile"]["tmp_name"],$upfile)){ //将传输文件的临时文件移动到创建的新文件 echo "Problem: Could not move file to destination directory"; exit; } } else{ echo "Problem: Possible file upload attack.Filename:"; echo $_FILES["username"]["name"]; exit; } echo "File uploaded seuucessfully<br/><br/>"; //对传输文件清除html和php标记 $contents = file_get_contents($upfile); //将文件内容提取为一个字符串; $contents = strip_tags($contents); //对该字符串擦除html和tags标记; file_put_contents($_FILES["userfile"]["name"],$contents); //将该字符串重新写入文件中; //在浏览器上显示传输的文本文件内容 echo "<p>Preview of uploaded file contents:<br/><hr>>"; echo nl2br($contents); echo "</br></hr>";3、使用目录函数
<?php $current_dir = "/uploads/"; //创建目录url对象 $dir = opendir($current_dir); //打开目录,结果返回一个目录对象 echo "<p>Upload directory is $current_dir</p>"; echo "<p>Directory Listing:</p><ul>"; while(($file = readdir($dir)) !== false){ //读取目录对象 if($file != "." && $file != ".."){ //过滤当前目录和上一级目录 echo "<li>$file</li>"; echo "<a href="filedetails.php?file=".$file."">".$file."</a><br/>"; } } echo "</ul>"; closedir($dir); //关闭目录; ?>②使用php的dir类
<?php $current_dir = "/uploads/"; //创建目录url对象 $dir = dir($current_dir); //创建dir对象 echo "<p>Handle is $dir->handle</p>"; echo "<p>Upload directory is $current_dir</p>"; echo "<p>Directory Listing:</p><ul>"; while(($file = $dir->read()) !== false){ //通过dir对象读取目录下的文件名 if($file != "." && $file != ".."){ echo "<li>$file</li>"; } } echo "</ul>"; $dir->close(); //关闭目录 ?>(2)使用scandir()函数对文本名称进行字母表的排序方式
<?php $current_dir = "/uploads/"; //创建目录url对象 $files1 = scandir($current_dir); //将指定目录下的文件名保存为一个数组,默认以字母升序排序 $files2 = scandir($current_dir,1); //将指定目录下的文件名保存为一个数组,以字母降序排序 echo "<p>Upload directory is $current_dir</p>"; echo "<p>Directory Listing in alphabetical order,ascending:</p><ul>"; foreach($files1 as $file1) { if($file1 != "." && $file1 != "..") echo "<li>$file1</li>"; } echo "</ul>"; ?>(3)获取当前目录的其他信息
$oldumask = umask(0); //重置当前权限码mkdir("/tmp/testing",0777); //创建目录umask($oldumask); //恢复当前权限码②rmdir():删除目录;
rmdir("/temp/testing");或rmdir("c:\tmp\testing");
while(($file = readdir($dir)) !== false){ echo "<a href="filedetails.php?file=".$file."">".$file."</a><br/>";}<?php $current_dir = "/uploads/"; $file = basename($file); //获取文件文件名 echo "<h1>Details of file</h1>"; echo "<h2>File data</h2>"; echo "File last accessed: ".date("j F Y H:i",fileatime($file))."<br>"; //返回最近访问的时间戳 echo "File last modifixed: ".date("j F Y H:i",filemtime($file))."<br>"; //返回最近修改的时间戳 $user = posix_getpwuid(fileowner($file)); //返回用户标识uid echo "File owner: ".$user["name"]."<br/>"; $group = posix_getgrgid(filegroup($file)); //返回组织标识gid echo "File group: ".$group["name"]."<br/>"; echo "File permissions: ".decoct(fileperms($file))."<br/>"; //返回8位的权限码 echo "File type".filetype($file)."<br/>"; //返回文件类型 echo "File size".filesize($file)."<br/>"; //返回文件字节数 echo "<h2>File Tests</h2>"; echo "is_dir?".(is_dir($file) ? "true":"false")."<br/>"; echo "is_executable?".(is_executable($file) ? "true":"false")."<br/>"; //判断文件是否可执行; echo "is_file?".(is_file($file) ? "true":"false")."<br/>"; echo "is_link?".(is_link($file) ? "true":"false")."<br/>"; echo "is_readable?".(is_readable($file) ? "true":"false")."<br/>"; echo "is_writable?".(is_writable($file) ? "true":"false")."<br/>"; ?>(2)更改文件属性