Blogger Information
Blog 16
fans 0
comment 1
visits 18547
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
常用文件操作,目录操作,文件的删除,复制与移动,文件上传- 2018-08-28
安丰的博客
Original
691 people have browsed it

实例

<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" VALUE="542388"  > <!--设定表单常亮大小-->
<!--action:处理表单的脚本地址,为空默认当前脚本 -->
<!--    --><?php //echo $_SERVER['PHP_SELF']?><!--  系统参数直接获取当前文件地址-->
<!--// --><?php //// echo htmlspecialchars($_SERVER['PHP_SELF']) ?><!-- 使用htmlspecialchars 保护现有文件地址不被串改-->
<!--enctype="multipart/form-data" :因为上传的是文件, 必须选择此类型-->

<fieldset><!-- /**表单域-->
    <Legend>文件上传</Legend>
    <input type="file" name="upload">
</fieldset>
<p align="center"><button type="submit">上传</button> </p>
</form>
<?php
//检测用户提交的类型
//if($_SERVER['REQUEST_METHOD']== 'POST'){
//    //检测文件是否被上传
//    if(isset($FILES['upload'])) {
//        //检测文件类似是否被允许 文件类型 jpg,jpeg,png
//        $allow = ['image/jpg', 'image/jpeg', 'image/png'];
//
//        if (in_array($FILES['upload']['type'], $allow))
//            //将文件上传至临时目录
//            $tempFile = $_FILES['upload']['temp_name'];//临时文件名
//        $destFile = $_FILES['upload']['name']; //目标文件名称
//        {
//            if (move_uploaded_file($tempFile, “s”upload / $destFile”)) {
//                echo '上传错误';
//            } else {
//                echo '上传目录出错';
//            }
//
//
//        }
//    查询文件上传方式是否正确
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 '上传成功';
            }else{
                echo'上传失败';
            }

        }else{
            echo '上传文件必须为照片类型';
        }
    }else{

        $error= $_FILES['upload']['error'];
            if ($error >0){
            echo '<p>错误提示是:<strong>';
                switch ($error){
                    case 1:
                        echo '文件超出大小';
                        break;
                    case 2:
                        echo '超过了表单常量大小';
                        break;
                    case 3:
                        echo '仅有部分上传';
                        break;
                    case 4:
                        echo '没有文件上传';
                        break;
                    default:
                        echo '未知错误';
                        break;
                }
            echo'</strong></p>';
        }
        else{
            echo '文件上传目录出错';
        }
    }

}else{
    echo '上传方式错误';
}


?>







<?php
/**
 * 文件上传
 * 系统变量
 * //编程: 文件上传与检测
 */
//$_GET,$_POST  客-户上传模式 上传文件大小 和账号密码等信息是否显示在地址栏 进行选择
//$_SERVER:保存当前服务器上的一些信息
//$_COOKIE,$_SESSION:客-户的一些会话信息
//$_FILES : 保存当前客-户操作数据
//PHP_SELF  :显示当前脚本名称
//$_SERVER['SCRIPT_NAME']:当前脚本名称

//echo'<pre>';
//var_dump($_SERVER);

运行实例 »

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

实例

//opendir('路径') '.':当前目录  ‘..’:上一级目录
$dir =opendir('../8.28') or die('打开失败');
echo '<hr>';
//$file 读取文件结果
while(false !=($file =readdir($dir))){
    print nl2br($file."\n");
}

echo '<hr>';

while(false !=($file =readdir($dir))){
   if ($file != '.' && $file != '..'){
       print nl2br($file."\n");
   }
}
echo '<hr>';
//目录扫描器
$fileArr= scandir('../8.23');
foreach($fileArr as $file) {
    if ($file != '.' && $file != '..') {
        print nl2br($file . "\n");
    }
}

运行实例 »

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


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!