13th day

今天是个好日子啊,学习了两个非常重要的知识点啊,分页和上传文件,不得不说这两个东西有点绕啊,需要慢慢理解消化!

依葫芦画瓢封装了一个上传文件分函数,分页基本上没有什么太大的问题了!

<?php
/**
 * 此文件用来封装一个上传文件的函数
 * @param array $file 上传的文件的信息(以为数组,包含5个元素)
 * @param array $allow 允许上传的文件类型
 * @param string &$error 引用传递,用来记录错误信息
 * @param string $path 文件上传的目录
 * @param int $maxsize 允许上传的文件的大小
 * @return mixed false/$newname 上传失败返回false,上传成功返回新文件名
 */

function upload($file,$allow,&$error,$path,$maxsize=1048576){
    // 1.先判断系统错误
    switch($file[‘error‘]){
        case 1:
            $error = "上传失败!超出了文件限制的大小!";
            return false;
        case 2:
            $error = "上传失败!超出了浏览器规定的文件大小!";
            return false;
        case 3:
            $error = "上传失败!文件上传不完整!";
            return false;
        case 4:
            $error = "上传失败!请选择要上传的文件!";
            return false;
        case 6:
        case 7:
            $error = "对不起,服务器繁忙,请稍后再试!";
            return false;
    }
    // 2.判断逻辑错误
    if($file[‘size‘] > $maxsize){
        $error = "上传失败!文件太大!";
        return false;
    }
    if(!in_array($file[‘type‘],$allow)){
        $error = "上传文件的类型不正确,正确的类型为:" . implode(‘,‘,$allow);
        return false;
    }
    // 3.移动临时文件到指定目录
    //获取新文件名
    $newname = randName($file[‘name‘]);
    $target = $path . ‘/‘ . $newname;
    $result = move_uploaded_file($file[‘tmp_name‘],$target);
    if($result){
        return $newname;
    }else{
        $error = "发生未知错误,上传失败!";
        return false;
    }
}

/**
 * 此函数用来生成一个随机的文件名(当前的年月日时分秒+6位随机数字+后缀名)
 * @param string $filename 原文件名
 * @return string $newname 新文件名
 */
function randName($filename){
    // 1.生成文件名的时间部分
    $newname = date("YmdHis");
    // 2.加上随机产生的6位数
    $str = "0123456789";
    for($i=0;$i<6;++$i){
        $newname .= $str[mt_rand(0,strlen($str)-1)];
    }
    // 3.加上文件的后缀名
    $newname .= strrchr($filename,‘.‘);
    return $newname;
}
时间: 2024-10-07 05:22:11

13th day的相关文章

December 13th 2016 Week 51st Tuesday

Life is a sail trip full of chances and challenges. 人生的旅途中充满了机遇和挑战. A boat sails on the sea, the vast sea, it may be lost in the boundless expanse of the sea, it may be overturned by the huge waves, it may be drifted along the winds and the currents.

October 13th 2016 Week 42nd Thursday

If the world seems cold to you, kindle fires to warm it. 若世界以寒相待,请点燃火堆以温暖相报. Kindle fires to warm the space around you, to light the space around you. Sometimes you may feel colder after kindling fires, don't worry, that just shows the cold world aro

August 13th 2016 Week 33rd Saturday

What makes life dreary is the want of motive. 没有目标与动力,生活便会郁闷无光. Without dreams and hope, there will be no motive, and life will be no future. I always feel that my life is dreary and depressed unsuccessful, just because there is no motive in my life.

June 13th 2017 Week 24th Tuesday

There are no regrets in life, just lessons. 人生中没有后悔,只有教训. Some people can learn from their past mistakes and never make the same mistake again. But some people only regret for their past failures and seldom learn lessons from their negative experienc

May 13th 2017 Week 19th Saturday

Mountains look beautiful from a distance. 远处看山山更美. This gnomic seems to circulate very long, its more universal version is the distance can produce the beauty, alleged far see a flower, see posse hemp nearly. Yes, the distance produces beauty, while

July 13th 2017 Week 28th Thursday

No dream is too big, and no dreamer is too small.梦想再大也不嫌大,追梦的人再小也不嫌小.Hold on to your dreams, but be careful that some of them may be too far away to be realized.Maybe they are too big for you, but if you can keep moving forward, maybe they will becom

July 13th, Week 29th Wednesday, 2016

Travel imparts new vigor to the mind. 旅行能给思想带来新的活力. Travel can give us opportunities to experience many different things, such as getting acquainted with different people, trying diffenernt life styles. In those experiences, we may understand the ess

November 13th, 2017 Week 46th Monday

Don't undermine your worth by comparing yourself with others. 别拿自己和他人比较,这只会降低你原有的价值. Honestly, I don't think it is merely negative to compare ourselves with others. The effects depend on how we react to our superiority and inferiority we get from suc

January 13th, 2018 Week 02nd Saturday

Anyone who has no spiritual aspirations is an idiot. 任何没有精神追求的人都是愚昧无知的人. Today I went to a bookshop with my girlfirend. I was surprised that there were so many people, not only students, but also many adults, who enjoyed reading there. Maybe we all h