码迷,mamicode.com
首页 > 其他好文 > 详细

task9图片文件处理函数代码

时间:2017-02-06 22:12:04      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:php   his   code   函数   文件上传   文件路径   als   第一个   字符串   

<?php
/*
upload($_FILES[‘pic‘]); 调用upload()函数
upload($file,$path=null,$type=array(‘image/png‘,‘image/jpeg‘,‘image/jpg‘,‘image/gif‘)){}; 定义upload函数
$file; 是一个数组变量
$file = array(5) {
  ["name"]=>
  string(8) "icon.png"
  ["type"]=>
  string(9) "image/png"
  ["tmp_name"]=>
  string(45) "/Applications/XAMPP/xamppfiles/temp/phpNqvAJe"
  ["error"]=>
  int(0)
  ["size"]=>
  int(24396)
}
$type = array(‘image/png‘,‘image/jpeg‘,‘image/jpg‘,‘image/gif‘); 把可选的图片类型定义为一个数组
is_uploaded_file($file[‘tmp_name‘]); 判断文件是否是通过HTTP POST上传过来的,可防止恶意访问
in_array($file[‘type‘],$type); 判断某个值是否在$type数组中
explode(".",$file[‘name‘]); 把字符串以.为分隔,打散为入栈数组
array_pop(explode(".",$file[‘name‘])); 以出栈的方式弹出数组中的第一个值
rtrim($path,‘/‘); 删除字符串最后的/和空格
is_null($path); 判断路径是否为空,为空则为真
move_uploaded_file($file[‘tmp_name‘],$filePath); 把文件移到新的地方,文件包括文件路径和文件名
*/
// echo "<pre>";
// var_dump($file);
function upload($file, $path = null, $type = array(‘image/png‘, ‘image/jpeg‘, ‘image/gif‘, ‘image/jpg‘)){
    //判断文件是否是通过HTTP POST上传过来的,防止恶意访问其他不让访问的页面
    if(is_uploaded_file($file[‘tmp_name‘])){
        //判断文件的类型
        if(!in_array($file[‘type‘], $type)){
            return false;
        }
        //判断文件的大小
        if($file[‘size‘] > 1024 * 1024 * 10){
            return false;
        }

        //生成一个要把图片移动到别处的新路径
        //以时间戳来给图片重新取一个名字,$file[‘name‘] = "icon.png";
        $filename = date(‘YmdHis‘).rand(100, 999);
        $ext = array_pop(explode(‘.‘, $file[‘name‘]));
        $filename = $filename.‘.‘.$ext;
        //判断用户有没有设置要把文件上传到的路径,默认为空
        if(is_null($path)){
            $path = $_SERVER[‘DOCUMENT_ROOT‘].‘/object/uploads/‘;
            $returnFile = $filename;
        }else{
            $path = rtrim($path, ‘/‘).‘/‘;
            $returnFile = $path.$filename;
        }
        //生成文件要上传到的新路径,文件路径包括路径和文件名
        $filePath = $path.$filename;
        //把文件上传到需要放置的新路径
        if(move_uploaded_file($file[‘tmp_name‘], $filePath)){
            return $returnFile;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

 

task9图片文件处理函数代码

标签:php   his   code   函数   文件上传   文件路径   als   第一个   字符串   

原文地址:http://www.cnblogs.com/htmlphp/p/6371896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!