Blogger Information
Blog 22
fans 1
comment 1
visits 22866
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php文件上传
forever浅笑
Original
735 people have browsed it

实例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>Document</title>
    <style> * { margin: 0; padding: 0; } form div { text-align: center; padding: 2%; } </style>
</head>
<body>
<?php
//Array
//(
//    [photo] => Array
//    (
//            [name] => sss_sg9u7ygh.js
//            [type] => application/javascript
//            [tmp_name] => C:\Users\Administrator\AppData\Local\Temp\phpB3A.tmp
//            [error] => 0
//            [size] => 10448
//        )
//
//)
//    print_r($_FILES);
if (isset($_FILES["photo"])) {
    if ($_FILES["photo"]["error"] > 0) {
        switch ($_FILES["photo"]["error"]) {
            case 1:
                exit("其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。");
                break;
            case 2:
                exit("其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。 ");
                break;
            case 3:
                exit("其值为 3,文件只有部分被上传。");
                break;
            case 4:
                exit("其值为 4,没有文件被上传。");
                break;
            case 6:
                exit("其值为 6,找不到临时文件夹。");
                break;
            case 7:
                exit("其值为 7,文件写入失败。");
        }
    }
    $uploadDir = './uploads/';
    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 755);
    }
    $fileInfo = explode('.', $_FILES["photo"]["name"]);
    $fileExt = array_pop($fileInfo);
    $ext = array('jpg', 'jpeg', 'bmp', 'gif', 'png');
    if (!in_array($fileExt, $ext)) {
        exit('文件格式不正确');
    }
    if (move_uploaded_file($_FILES["photo"]["tmp_name"], $uploadDir . $_FILES["photo"]["name"])) {
        exit('上传成功');
    } else {
        exit('上传失败');
    }


}
?>
<form method="post" enctype="multipart/form-data">
    <fieldset>
        <legend align="center">文件上传</legend>
        <p><strong>选择文件:</strong><input type="file" name="photo"></p>
    </fieldset>
    <div><input type="submit" value="上传"></div>
</form>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post