Blogger Information
Blog 36
fans 0
comment 1
visits 27891
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件上传
其琛的博客
Original
626 people have browsed it

代码如下

<!--文件上传请求必须是POST-->
<!--允许数据类型必须是multipart/form-data-->
<!--action三种写法-->
<!--1.空-->
<!--2.当前脚本-->
<!--3.现在用的  -->
<form action="<?php echo htmlspecialchars( $_SERVER['PHP_SELF'])?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="file-size" value="542488">
    <fieldset>
        <legend align="center">文件上传</legend>
        <p><strong>选择文件:</strong><input type="file" name="upload"></p>
    </fieldset>
    <p><button type="submit" name="submit">上传</button></p>
</form>
<?php
//$_FILES:二维数组
//$_FILES['当前上传文件的控件名称,input[name="upload"]']
//$_FILES['upload']['name']文件原始名称
//$_FILES['upload']['type']文件类型
//$_FILES['upload']['size']文件大小
//$_FILES['upload']['tmp_name']服务器上临时文件夹
//$_FILES['upload']['error']错误代码
if ($_SERVER['REQUEST_METHOD']=='POST') {
    //判断是否有文件上传
    if (isset($_FILES['upload'])) {
        $allow = ['image/jpg','image/jpeg', 'image/png'];
        if (in_array($_FILES['upload']['type'], $allow)) {
            if (move_uploaded_file($_FILES['upload']['tmp_name'], "upload/{$_FILES['upload']['name']}")) {
                echo '<script>alert("上传成功")</script>';
            }

        } else {
            echo '<script>alert("上传文件格式不对")</script>';
        }
    }
    if ($_FILES['upload']['error'] > 0) {
        echo '<p>错误原因:<strong>';
        switch ($_FILES['upload']['error']) {
            case 1:
                echo '文件超过大小';
                break;
            case 2:
                echo '文件超过了表单中常量设置的大小';
                break;
            case 3:
                echo '仅有部分文件被上传';
                break;
            case 4:
                echo '没有文件被上传';
                break;
            case 6:
                echo '没有可用的临时文件夹';
                break;
            case 7:
                echo '磁盘已满,写入失败';
                break;
            case 8:
                echo '上传意外中止';
                break;
            default:
                echo '未知错误';
                break;
        }
        echo '</strong></p>';
        if (file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])) {
            unlink($_FILES['upload']['tmp_name']);
        }
    }else {
        echo '1';}
}

?>

样式如下G6UPRSLYI(Z%]V8MWZFIZF0.png手写如下qq_pic_merged_1524475323899.jpg

Correction status:qualified

Teacher's comments:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!