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

php文件上传及错误代码总结

May 25, 2016 pm 04:55 PM

本文章总结了关于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.



永久链接:

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

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Comment réparer l'audio si vous n'entendez personne
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)