您的位置:首页 > 编程语言 > PHP开发

PHP学习练手(十二)

2016-01-07 20:12 861 查看

发送电子邮件

函数:

1、发送邮件函数: (subject中不能包含换行符;正文中每一行的长度都不能超过70,故用wordwrap函数进行隔断)

mail(to, subject, body, [headers])


2、字符串隔断函数

wordwrap(string, len)


代码:

email.php:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Me</title>
</head>
<body>
<h1>Contact Me</h1>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(!empty($_POST['name']) && !($_POST['email']) && !empty($_POST['comments']))
{
$body = "Name: {$_POST['name']}\n\nComments: {$_POST['comments']}";

//字符截断
$body = Wordwrap($body, 70);

mail('email@qq.com', 'Contact From Submission', $body, "From:{$_POST['email']}");

echo '<p><em>Thank you for contacting me. I will reply some day.</em></p>';

$_POST =array() ;	//将$_POST清空
}else{
echo '<p style="font-weight: bold; color: #C00"></p>';
}
}
?>
<p>Please fill out this form to contact me.</p>
<form action="email.php" method="post">
<p>Name: <input type="text" name="name" size="30" maxlength="60" value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>" /></p>
<p>Email Address: <input type="text" name="name" size="30" maxlength="80" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
<p>Comments: <textarea name="comments" rows="5" cols="30"><?php if(isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></p>
<p><input type="submit" name="submit" value="Send!" /></p>
</form>
</body>
</html>


运行:





处理文件上传

准备工作:

必须正确地设置PHP











必须有一个临时存储目录,它具有正确的权限



必须有一个最终存储目录,它具有正确的权限

注:与htdocs在同一级目录下,这样更安全



表单必要语法条件:

<form enctype="multipart" action="script.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
File <input type="file" name="upload" />
</form>


初始表单标签的enctype部分指示表单能够处理多种类型的数据,包括文件。如果想接受文件上传,就必须包括enctype! 提交表单必须使用POST方式。MAX_FILE_SIZE 隐藏输入框是一种表单限制元素,用于限制所选的文件可以有多大(以字节为单位),它必须出现在文件输入框之前。file输入框类型将在表单中创建合适的按钮

函数:

从临时目录转移到永久目录:
move_uploaded_file()


函数搜索数组中是否存在指定的值:
in_array()


文件超全局变量:
$_FILES


检查文件或目录是否存在:
file_exists()


查指定的文件名是否是正常的文件:
is_files()


删除文件:
unlink()


代码:

upload_image.php:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload an Image</title>
</head>
<body>
<?php #Script 11.2 - upload_image.php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{

if(isset($_FILES['upload']))
{
//允许的图片格式
$allowed = array('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/png', 'image/x-png');

if(in_array($_FILES['upload']['type'], $allowed))
{
$name = $_FILES['upload']['name'];
//解决文件名中文乱码问题
$name = iconv("UTF-8","gb2312", $name);
if(move_uploaded_file($_FILES['upload']['tmp_name'], "../../../../uploads/$name"))
{
echo '<p><em>The file has been upload!</em></p>';
}
}else{
echo '<p>Please upload a JPEG or PNG image</p>';
}
}

if($_FILES['upload']['error'] > 0)
{
echo '<p style="font-weight: bold; color: #C00">The file could not be uploaded because: <strong>';
switch ($_FILES['upload']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
break;
case 3:
print 'The file was only partially upload.';
break;
case 4:
print 'No File was upload.';
break;
case 6:
print 'No temporary folder was available.';
break;
case 7:
print 'Unable to write to the disk.';
break;
case 8:
print 'File upload stopped.';
break;
default:
print 'A system error occurred.';
break;
}
print '</strong></p>';
}

//若临时文件依然存在,则删除临时文件
if(file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']))
{
unlink($_FILES['upload']['tmp_name']);
}
}
?>

<form enctype="multipart/form-data" action="upload_image.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="534288" />
<fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded: </legend>
<p><b>File: </b><input type="file" name="upload" /></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit" /></div>
</form>
</body>
</html>


运行前uploads文件夹为空:



上传图片后:







当加载bmp文件时:



当不加载任何图片文件时:



处理文件上传之PHP与JS

函数:

返回一个数组,其中包含指定路径中的文件和目录:
scandir()


返回字符串的一部分:
substr()


函数用于获取图像尺寸,类型等信息:
getimagesize()


向客户端发送原始的 HTTP 报头:
header()


参考header解释:/article/5018476.html

把所有字符转换为小写:
strtolower()


返回指定文件的大小:
filesize()


该函数读入一个文件并写入到输出缓冲:
readfile()


设定用于一个脚本中所有日期时间函数的默认时区:
date_default_timezone_set()


函数用于对日期或时间进行格式化:
date()


代码:

function.js:

//Script 11.3 - function.js

function create_window(image, width, height)
{
width = width + 10;
height = height + 10;

if(window.popup && window.popup.closed)
{
window.popup.resizeTo(width, height);
}

var specs = "location=no, scrollbars=no, menubars=no, resizeable=yes, left=0, top=0, width=" + width + ", height=" + height;

var url="show_image.php?image="+image;

popup = window.open(url, "ImageWindow", specs);
popup.focus();

}


images.php:

注:此处省略PHP的结束标签。因为不经意在PHP的结束标签后面加了多余的空格或空行时,浏览器可能无法显示图片(因为浏览器接受到与Content-Length头匹配的X长度的图像数据后,还会多接受一点多余的数据)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Images</title>
<script type="text/javascript" charset="utf-8" src="../JS/function.js"></script>
</head>
<body>
<p>Click on an image to view it in a separate window.</p>
<ul>
<?php #Script 11.4 - image.php
$dir = "../../../../uploads";

$files = scandir($dir);
foreach ($files as $image) {
//substr()返回字符串的一部分
if(substr($image, 0, 1) != '.')
{

$image_size = getimagesize("$dir/$image");
$image_true_name = iconv("gb2312", "utf-8", $image);
$image_name = urlencode($image_true_name);

echo "<li><a href=\"javascript:create_window('$image_true_name', $image_size[0], $image_size[1])\">$image_true_name</a></li>\n";
}

}
?>
</ul>
</body>
</html>


show_image.php:

<?php
$name = false;

if(isset($_GET['image']))
{
$ext = strtolower(substr($_GET['image'], -4));
if(($ext == '.jpg') OR ($ext == 'jpeg') OR ($ext == '.png'))
{
$image_true_name = iconv("UTF-8","gb2312", $_GET['image']);
$image="../../../../uploads/$image_true_name";
//echo $image;
//$image = "../../../../uploads/{$_GET['image']}";
//echo $image;
if(file_exists($image) && is_file($image))
{
$name = $_GET['image'];
}
}

}

if(!$name)
{
$image = 'images/unavailable.png';
$name = 'unavailable.png';
}

$info = getimagesize($image);
$fs = filesize($image);

//发送信息头:
header ("Content-type: {$info['mime']}\n");
header ("Content-Disposition: inline; filename=\"$name\"\n");
header ("Content-Length: $fs\n");

readfile($image);


运行:





images1.php:(对images.php的增强)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Images</title>
<script type="text/javascript" charset="utf-8" src="../JS/function.js"></script>
</head>
<body>
<p>Click on an image to view it in a separate window.</p>
<ul>
<?php #Script 11.6 - image1.php

date_default_timezone_set('PRC');   //中国标准时间
$dir = "../../../../uploads";

$files = scandir($dir);
foreach ($files as $image) {
//substr()返回字符串的一部分
if(substr($image, 0, 1) != '.')
{

$image_size = getimagesize("$dir/$image");

$file_size = round((filesize("$dir/$image")) / 1024)."kb";

$image_date = date("F d, Y H:i:s", filemtime("$dir/$image"));

$image_true_name = iconv("gb2312", "utf-8", $image);
$image_name = urlencode($image_true_name);

echo "<li><a href=\"javascript:create_window('$image_true_name', $image_size[0], $image_size[1])\">$image_true_name</a> $file_size ($image_date)</li>\n";
}

}
?>
</ul>
</body>
</html>


运行:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: