PHP development file upload tutorial encapsulation function

First of all, we need to put all the processing file error information in one file

Next we will define a function file, function.php

We will use 3 files here, file.html doaction.php function.php

We won’t go into details about the first page, the code is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>文件上传</title>
</head>
<body>
    <form method="post" action="doaction1.php" enctype="multipart/form-data">
        上传文件:<input type="file" name="myfile"><br>
                  <input type="submit" value="上传文件">
    </form>
</body>
</html>

Let’s take a look at doaction.php

header ("Content-type: text/html; charset=utf-8");
$fileinfo = $_FILES['myfile'];
$allowEXT = array('jpg','png','jpeg ','gif');//File format
$maxfile = 2097152; //2M //How big is the file
$ext = pathinfo($fileinfo['name'],PATHINFO_EXTENSION);//Get the file type
$file = $fileinfo['error']; //Storage error information

We haven’t finished writing this file yet. When we encapsulate the function, we will introduce the encapsulated file into this file , and then call the function

Let’s look at the function.php file. The code is as follows:

<?php
    function files($file,$ext,$allowEXT,$maxfile,$size){
            //判断错误号
            if($file > 0){
                switch ($file){
                    case 1: $msg=  "上传文件超过了php 配置文件中 upload_max_filesize 的值";break;
                    case 2: $msg=  "上传文件超过max_file_size 大小";break;
                    case 3: $msg=  "文件部分被上传";break;
                    case 4: $msg=  "没有选择上传文件";break;
                    case 6: $msg=  "没有找到临时目录";break;
                    case 7: 
                    case 8: $msg=  "系统错误";break;
                }
                exit($msg);
            }
            //检测文件上传的类型
            if(!in_array($ext, $allowEXT)){
                exit('非法文件类型');
            }

            //检测上传文件的大小
            if($fileinfo['size']>$maxfile){
                exit('文件过大');
            }

            //检测是否是http post方式提交的
            if(!is_uploaded_file($fileinfo['$tmp_name'])){
                exit("文件不是以POST方式提交");
            }
            
    }
?>

With the above code, we have completed the encapsulation. The function has 4 parameters

Before we Speaking of doction.php, you need to introduce function.php, and then call the function

The complete code of the documentation.php file is as follows:

<?php
    header("Content-type: text/html; charset=utf-8");
    require_once("function.php"); 
    $fileinfo = $_FILES['myfile'];
    $allowEXT = array('jpg','png','jpeg','gif');//文件格式
    $maxfile = 2097152; //2M   //文件多大
    $ext = pathinfo($fileinfo['name'],PATHINFO_EXTENSION);//获取文件类型
    $file = $fileinfo['error']; //存放错误信息
    $size = $fileinfo['size'];
    files($file,$ext,$allowEXT,$maxfile,$size);
?>


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>文件上传</title> </head> <body> <form method="post" action="doaction1.php" enctype="multipart/form-data"> 上传文件:<input type="file" name="myfile"><br> <input type="submit" value="上传文件"> </form> </body> </html>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!