Blogger Information
Blog 39
fans 2
comment 2
visits 50545
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件常用操作—2018年8月28日
fighting的博客
Original
660 people have browsed it

                                                                      文件常用操作  

                                                    时间:2018年8月28日     天气:晴

编程: 目录遍历

 

实例

<?php
header('Content-type:text/html;charset=utf-8');
//1、目录传统操作:opendir(),readdir(),closedir();
//2、目录扫描器:scandir();
  $dir = opendir('../untitled7') or die('文件打开失败');
  while(false != ($file = readdir($dir))){
     if($file !="."&& $file !=".."){
         print nl2br($file ."\n");
     }
  }
  closedir($dir);
  echo '<hr>';
  $fileArr = scandir("D:");
  foreach ($fileArr as $file){
          if($file !="."&& $file !=".."){
              print nl2br($file ."\n");

      }
  }

运行实例 »

点击 "运行实例" 按钮查看在线实例

本机图片:

7U6(]4`ZU}M6X5@OZ)`KN88.png

编程: 文件上传与检测 

实例

<Meta http-equiv="Content-Type" Content="text/html; Charset=utf-8">
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="542488">
    <fieldset>
        <legend align="center">文件上传</legend>
        <input type="file" name="upload">
    </fieldset>
    <p align="center"><button type="submit" name="submit">上传</button></p>
</form>
<?php
header('Content-type:text/html;charset=utf-8');
//判断文件提交类型是否是post
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    //检测是否有文件并设置文件的上传格式
    if(isset($_FILES['upload'])){
        $allow = ['image/jpg','image/jpeg','image/png'];
    }
    if(in_array($_FILES['upload']['type'],$allow)){
        $tempFile = $_FILES['upload']['tmp_name'];
        $destFile = $_FILES['upload']['name'];
       if(move_uploaded_file($tempFile,"upload/$destFile")){
           echo '<script>alert("上传成功")</script>';
       }else{
           echo '<script>alert("上传格式错误!!!")</script>';
       }
    }
    if($_FILES['upload']['error']>0){
        echo '<p>错误的原因是:<strong>';
       switch ($_FILES['upload']['error']){
           case 1:
               echo '超过PHP_ini设置的大小';
               break;
           case 2:
               echo '超过文件表单中设置的大小';
               break;
           case 3:
               echo '仅有部分文件被上传';
               break;
           case 4:
               echo '没有文件被上传';
               break;
           case 6:
               echo '没有可用得临时文件';
               break;
           case 7:
               echo '磁盘已满,写入失败';
               break;
           case 8:
               echo '上传意外中止';
               break;
           default:
               echo '系统未知错误';
               break;
       }
       echo '</strong></p>';
        if (file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])) {
            unlink($_FILES['upload']['tmp_name']);
        }
    }
}else{
    exit('请求类型错误');
}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

本机运行图片:

7U6(]4`ZU}M6X5@OZ)`KN88.png总结:

1:文件与目录的常用操作:文件【fopen()-》fget()->fclose()】;目录【1、opendir()->readdir()->closedir();2、scandir()】;

2:文件常用的魔术常量:1、_FILE_文件或目录想管的系统常量;2、basename(_FILE_)当前文件名;3、dirname(_FILE_)当前目录名,平接注意加‘/’(__DIR__); 3、pathinfo(); 4、    DIRECTORY_SEPARATOR:路径分隔符。

3、文件的上传实战。

4、漏了好多课程,我的抓紧了。


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post