PHP判断上传文件是否为图片且保存为唯一命名

<?php
header("content-type:text/html;charset=utf-8");
$arr_ext = array("jpg","png","gif");
$index = strrpos($_FILES['upload']['name'],".");
$ext = substr($_FILES['upload']['name'],$index+1);
if(!in_array($ext,$arr_ext))
{
 echo "上传的文件不是图像";
 exit();
}else
{
 $src = $_FILES['upload']['tmp_name'];
 $dst = "./upload/".uniqid().".$ext";
 move_uploaded_file($src,$dst);
 echo "上传文件成功";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>判断上传文件是否为图片,为保存为唯一命名</title>
</head>
<body>
 <form name="form1" method="post" enctype="multipart/form-data" action="upload2.php">
 上传图片:<input type="file" name="upload">
 <input type="submit" value="提交表单">
 </form>
</body>
</html>

 

Continue Reading

PHP上传单个文件并保存至服务器

<?php
header("content-type:text/html;charset=utf-8");
if($_FILES['upload']['error']!=0)
{
 echo "上传文件发生错误";
 exit();
}
if($_FILES['upload']['size']>2*1024*1024)
{
 echo "上传文件大小超出要求";
 exit();
}
$src = $_FILES['upload']['tmp_name'];
$dst = "./upload/".$_FILES['upload']['name'];
if(!move_uploaded_file($src,$dst))
{
 echo "上传文件移动保存失败";
 exit();
}
echo "上传文件成功";
?>

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>文件上传</title>
</head>
<body>
 <form name="form1" method="post" enctype="multipart/form-data" action="upload.php">
 上传图片:<input type="file" name="upload">
 <input type="submit" value="提交表单">
 </form>
</body>
</html>

Continue Reading

PHP随机字母数字验证码

<?php
header("content-type:text/html;charset=utf-8");
//变量初始化
$str = "";
$filename = "./images/captcha.png";
$font = 5;
//生成随机验证码字符串
//array_merge合并一个或多个数组
$arr_list = array_merge(range('A','Z'),range(0,9));
//array_rand从数组中随机取出一个或多个单元
$index_list = array_rand($arr_list,8); //随机取8个下标
shuffle($index_list); //打乱数组
foreach($index_list as $value)
{
$str .= $arr_list[$value];
}
//创建画布
$img = imagecreatefrompng($filename);
//分配颜色,随机字体颜色
$color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
//获取画布和字体尺寸
$imgWidth = imagesx($img);
$imgHeight = imagesy($img);
$fontWidth = imagefontwidth($font);
$fontHeight = imagefontheight($font);
//计算字符串的起始坐标
$x = ($imgWidth -$fontWidth*strlen($str))/2;
$y = ($imgHeight - $fontHeight)/2;
//将验证码字符串写入画布上
imagestring($img,$font,$x,$y,$str,$color);
//输出图像
header("content-type:image/png");
imagepng($img,'./images/captcha_new.png');
imagedestroy($img);
?>

Continue Reading

PHP图像字符水印居中

<?php
//让图像上字符串居中
header("content-type:text/html;charset = utf-8");
//(1)创建画布
$filename = "./images/02.jpg";
$img = imagecreatefromjpeg($filename);
$str = "WELCOME to liuguofeng.com";
$font = 5;
//(2)分配颜色:
$color1 = imagecolorallocate($img,255,0,255);
$color2 = imagecolorallocate($img,255,0,255);
//(3)获取画布宽度和高度
$imgWidth = imagesx($img);
$imgHeight = imagesy($img);
//(4)获取字体尺寸
$fontWidth = imagefontwidth($font);
$fontHeight = imagefontheight($font);
//(5)计算字符串的起始坐标
$x = ($imgWidth-$fontWidth*strlen($str))/2;
$y = ($imgHeight-$fontHeight)/2;
//(6)写入一个字符串
imagestring($img,$font,$x,$y,$str,$color1);
header("content-type:image/jpeg");
imagejpeg($img);
//(7)关闭图像
imagedestroy($img);
?>

Continue Reading

PHP通过HTTP请求防盗链

anti-theft_chain.php

<?php
header("content-type:text/html;charset=utf-8");
if(isset($_SERVER['HTTP_REFERER']))
{
	//域名对比,一致下载,不一致跳转到错误页面
	//strpos: 查找字符串首次出现的位置
	if(strpos($_SERVER['HTTP_REFERER'],"http://guofeng.io")===0)
	{
		echo "您可以下载软件了";
	}else
	{
		echo "请从本网站下载";
	}
}
?>

anti-theft_chain.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>测试页面</title>
</head>
<body>
	<a href="http://guofeng.io/anti-theft_chain.php">测试页面</p>
</body>
</html>

Continue Reading

PHP隐藏下载地址

download.php

<?php
//获取传递的文件名
$filename = $_GET['filename'];
$pathname = "$./download/$filename";
//以只读方式打开文件
$handle = fopen($pathname,"rb");
//返回数据的内容类型
//'octet-stream'八位二进制流
//'content-desposition:attachment'以附件形式打开
header("content-type:application/octet-stream");
header("content-despositon:attachment;filename=$filename");
//循环读取指定大小的内容,并输出
while($str=fread($handle,1024))
{
echo $str;
}
//关闭文件
fclose($handle);
?>

download.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<a href="download.php?filename=test.jpg">下载图片</a>
<br>
<a href="download,php?filename=test.mp4">下载视频</a>
</body>
</html>

Continue Reading
Close Menu