search
Homephp教程php手册php文件上传及错误代码总结

本文章总结了关于php文件上传及错误代码的介绍,有需要学习的朋友可参考一下本文章。

上传操作代码

 代码如下 复制代码




   
   


 
    if($_FILES['myfile']['error'] > 0) {      //判断文件是否可以成功上传到服务器,0表示上传成功
        echo 'Error: ';
 
        switch ( $_FILES['myfile']['error']  ) {
            case UPLOAD_ERR_OK:
                $response = 'There is no error, the file uploaded with success.';
                break;
            case UPLOAD_ERR_INI_SIZE:
                $response = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
                break;
            case UPLOAD_ERR_FORM_SIZE:
                $response = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
                break;
            case UPLOAD_ERR_PARTIAL:
                $response = 'The uploaded file was only partially uploaded.';
                break;
            case UPLOAD_ERR_NO_FILE:
                $response = 'No file was uploaded.';
                break;
            case UPLOAD_ERR_NO_TMP_DIR:
                $response = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
                break;
            case UPLOAD_ERR_CANT_WRITE:
                $response = 'Failed to write file to disk. Introduced in PHP 5.1.0.';
                break;
            case UPLOAD_ERR_EXTENSION:
                $response = 'File upload stopped by extension. Introduced in PHP 5.2.0.';
                break;
            default:
                $response = 'Unknown error';
                break;
        }
        echo $response;
         
         
        exit;       //如果$_FILES['myfile']['error']大于0都是有错误,输出错误信息并退出程序
    }
     
     //获取上传文件的MIME类型中的主类型和子类型
    list($maintype,$subtype)=explode("/",$_FILES['myfile']['type']);    
     
    if ($maintype=="text") {    //通过主类型限制不能上传文本文件,例如.txt .html .php等文件文件
     
        echo '问题: 不能上传文本文件。';
        exit;                //如果用户上传文本文件则退出程序
         
    }
 
    $upfile = './uploads/'.time().$_FILES['myfile']['name'];     //定义上传后的位置和新文件名
     
    if ( is_uploaded_file($_FILES['myfile']['tmp_name']) ) {     //判断是否为上传文件
     
        if ( !move_uploaded_file($_FILES['myfile']['tmp_name'], $upfile) ) {   //从移动文件
            echo '问题: 不能将文件移动到指定目录。';
            exit;
        }
         
    }else{
         
        echo '问题: 上传文件不是一个合法文件: ';
        echo $_FILES['myfile']['name'];
        exit;
         
    }
 
    echo '文件 :  '.$upfile.'  上传成功, 大小为 : ' .$_FILES['myfile']['size'].'!
';   //如果文件上传成功则输出
?>

html

 代码如下 复制代码

br />      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


 
 


 

         
          选择文件:
         
     


一些常见的上传文件时错误代码

0 | UPLOAD_ERR_OK         | 文件成功上传
1 | UPLOAD_ERR_INI_SIZE   | Size exceeds upload_max_filesize in php.ini.
2 | UPLOAD_ERR_FORM_SIZE  | Size exceeds MAX_FILE_SIZE specified in HTML form.
3 | UPLOAD_ERR_PARTIAL    | 文件没有完整上传
4 | UPLOAD_ERR_NO_FILE    | 没有上传文件
5 | UPLOAD_ERROR_E        | As expliained by @Progman, removed in rev. 81792
6 | UPLOAD_ERR_NO_TMP_DIR | 找不到临时文件夹
7 | UPLOAD_ERR_CANT_WRITE | 磁盘不可写
8 | UPLOAD_ERR_EXTENSION  | File upload stopped by extension.



永久链接:

转载随意!带上文章地址吧。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools