php文件上传接口及文件上传错误服务器配置

一:上传表单

<form enctype="multipart/form-data" action="doFileUp.php" method="POST">

    <!-- MAX_FILE_SIZE must precede the file input field 
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> -->
    <!-- Name of input element determines name in $_FILES array -->
    
    Send this file: <input name="file" type="file" />
    <input type="submit" value="Send File" />
</form>

二:上传处理文件php

<?php
 
    //file dir
    $uploaddir = 'upload/';
    //file name
    $uploadfile = $uploaddir . basename($_FILES['file']['name']);
    //upload result
    $result = array();
    //begain upload
    if ($_FILES["file"]["error"] > 0)
    {
        $error = "file upload error";
        array_push($result,0,$error,$_FILES);
    }
    else
    {
         if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo json_encode($_FILES["file"]["name"] . " already exists. ");
        }
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
            $error = "file upload success";
            array_push($result,1,$error,$_FILES);
      }
    }
    
    echo json_encode($result);
    
    

?>

三:常见错误配置

其中$_FILES['userfile']['error']的可以有下列取值和意义: 
    0——没有错误发生,文件上传成功。  
    1——上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。  
    2——上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。  
    3——文件只有部分被上传。  
    4——没有文件被上传。  

参考:http://www.chinaunix.net/old_jh/27/667690.html

原文地址:https://www.cnblogs.com/cocoajin/p/3491371.html