图片上传

--------------------------html------------------------------------------------------html-------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>图片上传</title>
</head>
<body>
<form action="shc.php" enctype="multipart/form-data" method="POST">
        <input type="hidden" name="max_file_size" value="33554432">
        <input type="file" name="file" id="file">
        <input type="submit" name="submit" value="上传" οnclick="" />
    </form>
    <script type="text/javascript">
        function subup(){
            var file=document.getElementById("file").value;
            if(file==''){alert("选择文件...");return false;}
        }
    </script>
</body>
</html>

--------------------------php------------------------------------------------------php--------------------------------

<?php


//上传文件类型列表
$uptypes=array(
    'image/jpg','image/jpeg','image/png','image/pjpeg',
    'image/gif','image/bmp','image/x-png'
);


$width_size=500;   //规定图片最大宽度
$height_size=500;   //规定图片最大高度
$max_file_size=417200;   //上传文件大小限制, 单位BYTE
$uploads="uploads/";   //上传文件路径


$imgpreview=1;   //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=1/2;   //缩略图比例


//检查是否是Post提交
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{


    //是否存在文件
    if (!is_uploaded_file($_FILES["file"]['tmp_name']))exit("文件不存在");


    $file = $_FILES["file"];


    $filename=$file["tmp_name"];    //获取临时文件名
    $image_size = getimagesize($filename);   //获取图片相关信息


    //判断图片像素是否超过设定值
    if($image_size[0]>$width_size||$image_size[1]>$height_size)exit("图片超过规定像素");


    //检查文件大小
    if($max_file_size < $file["size"])exit("文件太大");


    //检查文件类型
    if(!in_array($file["type"], $uptypes))exit("文件类型不符");


    //检查存储路劲
    if(!file_exists($uploads))mkdir($uploads);


    $pinfo=pathinfo($file["name"]);   //获取文件名相关信息
    $ftype=$pinfo['extension'];   //获取文件扩展名(后缀)
    $destination = time().".".$ftype;   //以时间戳命名新文件


    //检查是否有同名文件
    if (file_exists($destination) && $overwrite != true)exit("同名文件已存在");


    //返回上传状态
    if(!move_uploaded_file ($filename,$uploads.$destination))exit("移动出错");




    //生成缩略图
    if($imgpreview==1){
     echo "<img src=\"".$uploads.$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize).">";
    }
}
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值