Home >Backend Development >PHP Tutorial >PHP sample code to determine the size of uploaded files

PHP sample code to determine the size of uploaded files

WBOY
WBOYOriginal
2016-07-25 08:57:591474browse
This article introduces a piece of code that uses PHP to determine the size of uploaded files. Friends in need can refer to it.

The code is as follows:

<?php
/**
* 判断上传文件大小
* edit bbs.it-home.org
*/

function imageUpload()
{
if (is_uploaded_file($_FILES['logo']['tmp_name'])) {
    //$_FILES是一个关于图片信息的数组
    $upfile = ($_FILES["logo"]); //赋值
    $suffix=substr($upfile["name"],strrpos($upfile["name"],"."));
    $name = mktime() .rand().$suffix; //原文件名
    $type = $upfile["type"]; //文件类型
    $size = $upfile["size"]; //文件大小
    $tmp_name = $upfile["tmp_name"]; //临时文件名
    $error = $upfile["error"]; //上传相关错误代码
   $str=getimagesize($tmp_name); 
 
   $mode="/width=\"(.*)\" height=\"(.*)\"/";
    preg_match($mode,$str[3],$arr);
    if($arr[1]>120 || $arr[2]>60){

echo "";
exit();
    }
       
    switch ($type) {
case 'image/pjpeg' :
    $ok = 1;
    break;
case 'image/jpeg' :
    $ok = 1;
    break;
case 'image/gif' :
    $ok = 1;
    break;
    //case 'image/x-png' :
    //    $ok = 1; //IE为x-png;FOX为png
    //    break;
    }
    if ($ok && $error == '0') {
       move_uploaded_file($tmp_name, "c:/" . $name);
       return $name;
    }
} else {
    return null;
}
}
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn