Blogger Information
Blog 31
fans 0
comment 0
visits 24407
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Day39-2018/1/14(文件上传,单例模式)
SmallKing的博客
Original
698 people have browsed it
  1. 文件上传;

<?php
header("content-type:text/html;charset=utf-8");
?>
<form action="" method="post" enctype="multipart/form-data">
    <p><label for=""><input type="file"name="upload"></label></p>
    <p><button>上传</button></p>
</form>
<?php
/**
 * 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不用设置
 * 可以先用phpinfo()函数查看一下
 *
 * 上传文件的所有信息保存在超全局变量
 */
//检测是否提交了表单
if($_SERVER['REQUEST_METHOD']=='POST'&&isset($_FILES['upload'])){
//    判断文件是否上传
    if(is_uploaded_file($_FILES['upload']['tmp_name'])){
        //获取上传文件信息
        $name=$_FILES['upload']['name'];
        $type=$_FILES['upload']['type'];
        $size=$_FILES['upload']['size'];
        $tmp_name=$_FILES['upload']['tmp_name'];
        $error=$_FILES['upload']['error'];
//        设定允许上传的图片类型
        $imgType=['image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png','image/gif'];
//        判断上传类型
        if(in_array($type,$imgType)) {
            echo "================<br>";
            echo "上传文件名称是:".$name."<br>";
            echo "上传文件类型是:".$type."<br>";
            echo "上传文件大小是:".$size."<br>";
            echo "上传后系统返回的值是:".$error."<br>";
            echo "上传文件的临时存放路径是:".$tmp_name."<br>";
            //储存文件src
            $destination='upload/'.$name;
            //文件名重新编码防止文件名乱码
            $name=iconv("UTF-8","gb2312", $name);
            if (move_uploaded_file($tmp_name,'upload/'.$name)){
                echo "文件上传成功,上传图片为:<br>";
                echo "<img src='".$destination."'><br>";
            }else echo "文件上传失败";
        }else echo '<p style="color:red">"请上传jpg,gif,png等格式的图片!";</p>';
        echo "================<br/>";
        //显示error信息
        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;
            }
        }

    }

    QQ图片20180115154211.png

2.单例模式;

<?php

//2.单例模式;
class demo1{
    //静态变量保持全局实例
    private static $instance=null;
    //私有化构造函数
    private  function __construct()
    {
    }
    //私有化克隆函数
    private function __clone()
    {

    }
    //设定对外唯一接口
    public static function getInstance(){
        if (!(self::$instance instanceof self)){
            self::$instance=new self();
        }
        return self::$instance;

    }
}
$a1=demo1::getInstance();
$a2=demo1::getInstance();
$a3=demo1::getInstance();
$a4=demo1::getInstance();
$a5=demo1::getInstance();
echo '<pre>';
var_dump($a1,$a2,$a3,$a4,$a5);

练习一.png

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