Blogger Information
Blog 34
fans 0
comment 0
visits 33684
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php之文件上传
Serendipity-Ling
Original
899 people have browsed it
<?php
/**
 *文件上传
 */

//屏蔽错误信息
error_reporting(E_ALL ^ E_NOTICE);

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    //确认是否上传文件
    if (isset($_FILES['upload']))
    {
        //设置允许上传文件的类型
        $allowed = ['image/jpeg','image/JPG','image/X-PNG','image/x-png','image/PNG'];
        //检查用户文件是否允许上传
        if (in_array($_FILES['upload']['type'],$allowed))
        {
            //将目标文件从临时目录移动到目标目录中
            $tmpFile = $_FILES['upload']['tmp_name'];
            if (move_uploaded_file($tmpFile,__DIR__.'/uploads'.$_FILES['upload']['name']))
            {
                echo '<h3>上传成功</h3>';
            }else
            {
                echo '<h3>上传失败</h3>';
            }
        }
    }
}


//输出错误信息
if ($_FILES['upload']['error']> 0 )
{
    switch ($_FILES['upload']['error'])
    {
            case 1:
                print '<p style="color:red">文件大小超过了PHP.INI中规定的大小</p>';
                break;
            case 2:
                print '<p style="color:red">文件大小超过了表单中规定的大小</p>';
                break;
            case 3:
                print '<p style="color:red">文件仅上传不完整</p>';
                break;
            case 4:
                print '<p style="color:red">没有文件被上传</p>';
                break;
            case 6:
                print '<p style="color:red">没有创建文件的临时文件夹</p>';
                break;
            case 7:
                print '<p style="color:red">无法写入磁盘</p>';
                break;
            case 8:
                print '<p style="color:red">文件上传被终止</p>';
                break;
            default:
                print '<p style="color:red">意外错误,请检查后重新上传~~</p>';
                break;
    }
}

//删除临时文件
if (file_exists($tmpFile) && is_file($tmpFile))
{
    unlink($tmpFile);
}









?>
<!--前端脚本-->
<h3>文件上传</h3>
<form action="" method="post" enctype="multipart/form-data">
    <div><input type="hidden" name="MAX_FILE_SIZE" value="100000"></div>
    <div><input type="file" name="upload"></div>
    <div><input type="submit" value="上传"></div>
</form>

在php.ini中要配置好这些文件

1.file_uploads = true;允许上传文件
2.max_input_time=30;允许脚本最长运行时间
3.post_max_size=8M;允许POST数据量大小
4.upload_max_filesize= 2M;允许上传的文件大小
5.upload_tmp_dir=c:\\temp;文件的临时目录,mac/linux不用设置

超全局变量

1.$_FILES

2.__DIR__:与dirname()功能相似,该方法返回的是你这个脚本的绝对路径

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