PHP development file upload tutorial: processing uploaded files (2)

First, let’s take a look at what the upload error number means

Then let’s test

0——No error occurred, the file was uploaded successfully. The default is 0, you can upload files

1 - The uploaded file exceeds the value limited by the upload_max_filesize option in php.ini.
2——The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
3——Only part of the file was uploaded

4——No file was uploaded.

6——Temporary directory not found

7——System error

Let’s write the statement to handle the error

<?php
    header("Content-type: text/html; charset=utf-8"); 
    $fileinfo = $_FILES['myfile'];
    $filename = $fileinfo['name'];//名称
    $type = $fileinfo['type'];//类型
    $tmp_name = $fileinfo['tmp_name'];//路径
    $size = $fileinfo['size'];//图片尺寸
    $error= $fileinfo['error'];

    //判断错误号,等于0的时候上传成功
    if($error == 0){
        if(copy($tmp_name,"uploads/".$filename)){
            echo "文件".$filename."上传成功";
        }else{
            echo "文件".$filename."上传失败";
        }

    }else{
        switch ($error){
            case 1: echo "上传文件超过了php 配置文件中 upload_max_filesize 的值";break;
            case 2: echo "上传文件超过max_file_size 大小";break;
            case 3: echo "文件部分被上传";break;
            case 4: echo "没有选择上传文件";break;
            case 6: echo "没有找到临时目录";break;
            case 7: 
            case 8: echo "系统错误";break;
        }
    }

?>

Get it first The data submitted by the form is the image information

In the previous section, we output the information of uploading 1.jpg, stored the information, and judged the error. Only when it is 0, the image was uploaded successfully.

Continuing Learning
||
<?php header("Content-type: text/html; charset=utf-8"); $fileinfo = $_FILES['myfile']; $filename = $fileinfo['name'];//名称 $type = $fileinfo['type'];//类型 $tmp_name = $fileinfo['tmp_name'];//路径 $size = $fileinfo['size'];//图片尺寸 $error= $fileinfo['error']; //判断错误号,等于0的时候上传成功 if($error == 0){ if(copy($tmp_name,"uploads/".$filename)){ echo "文件".$filename."上传成功"; }else{ echo "文件".$filename."上传失败"; } }else{ switch ($error){ case 1: echo "上传文件超过了php 配置文件中 upload_max_filesize 的值";break; case 2: echo "上传文件超过max_file_size 大小";break; case 3: echo "文件部分被上传";break; case 4: echo "没有选择上传文件";break; case 6: echo "没有找到临时目录";break; case 7: case 8: echo "系统错误";break; } } ?>
submitReset Code