PHP学习练手(十二)


发送电子邮件



函数:
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>  

运行:
这里写图片描述

这里写图片描述


处理文件上传


准备工作:

  1. 必须正确地设置PHP
    这里写图片描述

    这里写图片描述

    这里写图片描述

    这里写图片描述

    这里写图片描述

  2. 必须有一个临时存储目录,它具有正确的权限
    这里写图片描述

  3. 必须有一个最终存储目录,它具有正确的权限
    注:与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输入框类型将在表单中创建合适的按钮

函数:

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

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

  3. 文件超全局变量:$_FILES

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

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

  6. 删除文件: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


函数:

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

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

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

  4. 向客户端发送原始的 HTTP 报头:header()
    参考header解释:http://www.cnblogs.com/fengzheng126/archive/2012/04/21/2461475.html

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

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

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

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

  9. 函数用于对日期或时间进行格式化: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>

运行:
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值