当前位置: 编程技术>php
本页文章导读:
▪php 用户注册的实例代码 一个PHP的用户注册表单,负责接收用户输入的注册信息,点提交按钮后,显示刚刚提交的信息。
1,html部分
<html>
<head>
<title>用户注册-www.</title>
</head>
<body>
.........
▪php 自定义函数的例子 1,php自定义函数的例子
<html>
<head>
<title>定义一个函数</title>
</head>
<body>
<?php
function bighello(){
print "<h1>HELLO!</h1>";
}
bighello();
?>
</body>
.........
▪php图片文件上传类(可自动生成缩略图) php实现的图片文件上传类,代码:
<?php
/**
功 能:文件上传类 支持文件夹自动分组保存;
创建类:参数(文件域,文件原名,文件大小);
$myupload = new upfileClass($upfile,$upfile_name,$upfile_size);
$myu.........
[1]php 用户注册的实例代码
来源: 互联网 发布时间: 2013-12-24
一个PHP的用户注册表单,负责接收用户输入的注册信息,点提交按钮后,显示刚刚提交的信息。
1,html部分
<html>
<head>
<title>用户注册-www.</title>
</head>
<body>
<h2 align="center">User Registration Form</h2>
<form name="forml" method="post" action="/blog_article/userReg.html" enctype="multipart/form-data">
<table width="53%" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td width="49%">Name</td>
<td colspan="2">
<div align="left"
<input type="text" name="name" size="25" maxlength="25">
</div>
</td>
</tr>
<tr>
<td width="49%"> </td>
<td height="2" colspan="2"> </td>
</tr>
<tr>
<td width="49%" height="57">Address</td>
<td height="57" colspan="2">
<textarea name="address" cols="25" rows="4"> </textarea>
</td>
</tr>
<tr>
<td width="49%"> </td>
<td height="2" colspan="2"> </td>
</tr>
<tr>
<td width="49%"> Date of Birth</td>
<td height="2" colspan="2">
<select name=birth_month>
<option selected value=1>January
<option value=2> February
<option value=3>March
<option value=4>April
<option value=5>May
<option value=6>June
<option value=7>July
<option value=8>August
<option value=9>September
<option value=10>October
<option value=11>November
<option value=12>December
</select>
<select name=birth_day>
<option selected value=1>01
<option value=2>02
<option value=3>03
<option value=4>04
<option value=5>05
<option value=6>06
<option value=7>07
<option value=8>08
<option value=9>09
<option value=10>10
<option value=11>11
<option value=12>12
<option value=13>13
<option value=14>14
<option value=15>15
<option value=16>16
<option value=17>17
<option value=18>18
<option value=19>19
<option value=20>20
<option value=21>21
<option value=22>22
<option value=23>23
<option value=24>24
<option value=25>25
<option value=26>26
<option value=27>27
<option value=28>28
<option value=29>29
<option value=30>30
<option value=31>31</option>
</select>
<input maxlength=4 name=birth_year size=4>
(Year)</td>
</tr>
<tr>
<td width="49%" height="2"> </td>
<td height="2" width="17%"> </td>
<td height="2" width="34%"> </td>
</tr>
<tr>
<td width="49%">Gender</td>
<td height="2" width="17%"><input type="radio" name="gender" value="M">Male </td>
<td height="2" width="34%"><input type="radio" name="gender" value="F">Female </td>
</tr>
<tr>
<td width="49%" height="5"> </td>
<td height="5" colspan="2"> </td>
</tr>
<tr>
<td width="49%">Music Preference </td>
<td height="2" colspan="2">
<table width="100%" border="0">
<tr>
<td>
<input type="checkbox" name="pop" value="1">
Pop </td>
<td>
<input type="checkbox" name="rock" value="1">
Rock </td>
</tr>
<tr>
<td>
<input type="checkbox" name="jazz" value="1">
Jazz </td>
<td>
<input type="checkbox" name="metal" value="1">
Metal </td>
</tr>
<tr>
<td>
<input type="checkbox" name="instrumental" value="1">
Instrumental </td>
<td> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="49%" height="2"> </td>
<td colspan="2" height="2"> </td>
</tr>
<tr>
<td colspan="3">
<div align="center">
<input type="submit" name="Submit" value="Submit">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
2,接收用户注册信息 userReg.php。
<html>
<head>
<title>用户注册--www.</title>
</head>
<body>
<h2> Dear
<?php
$name = $_POST['name'];
$address = $_POST['address'];
$birth_month= $_POST['birth_month'];
$birth_day = $_POST['birth_day'];
$birth_year = $_POST['birth_year'];
$gender = $_POST['gender'];
$pop = $_POST['pop'];
$jazz = $_POST['jazz'];
$rock = $_POST['rock'];
$metal = $_POST['metal'];
$instrumental = $_POST['instrumental'];
if ($gender=='M') {
echo "Mr.";
} elseif ($gender=='F') {
echo "Ms.";
}
echo " ", $name;
?> ! 您的输入信息如下: </h2>
<table border="1">
<tr>
<td>Address </td>
<td>
<?php echo $address; ?>
</td>
</tr>
<tr>
<td>Date of Birth </td>
<td>
<?php echo $birth_month, " ", $birth_day, " ", $birth_year; ?>
</td>
</tr>
<tr>
<td colspan=2>请检查注册信息:
<?php
if (!(empty($pop))) {
if ($pop==1){
echo " Pop ";
}
}
if (!(empty($jazz))) {
if ($jazz==1) {
echo " Jazz ";
}
}
if (!(empty($rock))) {
if ($rock==1){
echo " Rock ";
}
}
if (!(empty($instrumental))) {
if ($instrumental==1) {
echo " Instrumental ";
}
}
if (!(empty($metal))) {
if ($metal==1){
echo " Metal ";
}
}
?>
</td>
</tr>
</table>
</body>
</html>
[2]php 自定义函数的例子
来源: 互联网 发布时间: 2013-12-24
1,php自定义函数的例子
<html>
<head>
<title>定义一个函数</title>
</head>
<body>
<?php
function bighello(){
print "<h1>HELLO!</h1>";
}
bighello();
?>
</body>
</html>
2,在页面上打印文本的自定义函数
<HTML>
<HEAD>
<TITLE>打印文本-www.</TITLE>
</HEAD>
<BODY>
<?php
function textonweb ($content, $fontsize) {
echo "<FONT SIZE=$fontsize>$content</FONT>";
}
textonweb ("A <BR>", 7);
textonweb ("AA. <BR>", 3);
textonweb ("AAA. <BR>", 3);
textonweb ("AAAA! <BR>", 3);
?>
</BODY>
</HTML>
3,声明一个简单的php自定义函数
<?php
function aFunction(){
print "音乐是生活的一种方式";
}
aFunction();
?>
4,从字符串创建自定义函数
<?
$lambda =create_function('$a,$b','return(strlen($a)-strlen($b));');
$array = array('really long string here,boy', 'this', 'middling length',
'larger');
usort($array,$lambda);
print_r($array);
?>
[3]php图片文件上传类(可自动生成缩略图)
来源: 互联网 发布时间: 2013-12-24
php实现的图片文件上传类,代码:
<?php
/**
功 能:文件上传类 支持文件夹自动分组保存;
创建类:参数(文件域,文件原名,文件大小);
$myupload = new upfileClass($upfile,$upfile_name,$upfile_size);
$myupload->savefile(); # 保存方法 并返回保存路径附带文件名;
@ echo MakeBuild($BuildFile,$newFile,$File_width);
生成指定文件的缩略图;
$myupload->MakeBuild("images/a.jpg","news/b.jpg","100");
*/
class upfileClass
{
var $upfile, $upfile_name, $upfile_size;
var $new_upfile_name; # 上传后的文件名称 ;
var $fleth, $fileExtent; # 文件扩展名(类型) ;
var $f1, $f2, $f3; # 文件保存路径(多级) upfiles/2008-01/08/;
var $filename; # 文件(带路径) ;
var $maxSize, $File_type; # 允许上传文件的大小 允许上传文件的类型 ;
var $BuildFile,$newFile,$File_width,$File_height,$rate;
function upfileClass($upfile,$upfile_name,$upfile_size)
{
$this->upfile = $upfile;
$this->upfile_name = $upfile_name;
$this->upfile_size = $upfile_size;
$this->new_upfile_name = $this->CreateNewFilename($this->upfile_name);
$this->f1 = "upfiles";
$this->f2 = $this->f1."/".date('Y')."-".date('m');
$this->f3 = $this->f2."/".date('d');
$this->filename = $this->f3 . "/" . $this->new_upfile_name;
$this->maxSize = 500*1024; # 文件大小 500KB
$this->File_type = "gif/jpg/jpeg/png"; # 允许上传的文件类型
}
# 创建新文件名 (原文件名)
function CreateNewFilename($file_name)
{
$this->fleth = explode(".",$file_name);
$this->fileExtent = $this->fleth[(int)count($this->fleth)-1]; # 获取文件后缀;
$tmpstr = date('YmdHis') . "." .$this->fileExtent; # 创建新文件名;
return $tmpstr;
}
# 检测文件类型是否正确
function chk_fileExtent()
{
$iwTrue = 0;
$fle = explode("/",$this->File_type);
for($i=0; $i < count($fle); $i++){
if( $this->fileExtent == $fle[$i] )
{
$iwTrue = (int) $iwTrue + 1;
}
}
if( $iwTrue == 0 ){
$this->msg("文件不符合 ".$this->File_type." 格式!");
}
}
# 提示错误信息并终止操作
function msg($Error)
{
echo "<script language=/"javascript/">/n";
echo " alert('".$Error."');/n";
echo " window.history.back();/n";
echo "</script>/n";
die();
}
# 保存文件
function savefile()
{
$this->chk_fileExtent();
$this->chk_fileSize();
$this->CreateFolder( "../".$this->f1 );
$this->CreateFolder( "../".$this->f2 );
$this->CreateFolder( "../".$this->f3 );
return $this->chk_savefile();
}
# 检测上传结果是否成功
function chk_savefile()
{
$copymsg = copy($this->upfile,"../".$this->filename);
if( $copymsg ){
return $this->filename;
}
else{
$this->msg("文件上传失败! /n/n请重新上传! ");
}
}
# 创建文件夹
function CreateFolder($foldername)
{
if( !is_dir($foldername) ){
mkdir($foldername,0777);
}
}
# 检测文件大小
function chk_fileSize()
{
if( $this->upfile_size > $this->maxSize ){
$this->msg("目标文件不能大于". $this->maxSize/1024 ." KB");
}
}
# 删除文件($filePath 文件相对路径)
function Deletefile($filePath)
{
if( !is_file($filePath) ){
return false;
}
else{
$ending = @unlink($filePath);
return $ending;
}
}
/*
函数:生成缩略图
MakeBuild("images/a.jpg","news/b.jpg","100");
参数:
echo $BuildFile; 原图 带路径
echo $newFile; 生成的缩略图 带路径
echo $File_width; 缩略图宽度值
echo $File_height; 缩略图高度值 (默认为宽度的比例值)
echo $rate; 缩略图象品质;
*/
function MakeBuild($BuildFile,$newFile,$File_width,$File_height=0,$rate=100)
{
if(!is_file($BuildFile)){
$this->msg("文件 ".$BuildFile." 不是一个有效的图形文件!/n/n系统无法生成该文件的缩略图!");
return false;
}
$data = GetImageSize($BuildFile);
switch($data[2]){
case 1:
$im = @ImageCreateFromGIF($BuildFile);
break;
case 2:
$im = @ImageCreateFromJPEG($BuildFile);
break;
case 3:
$im = @ImageCreateFromPNG($BuildFile);
break;
}
if(!$im){
return false;
}
else{
$srcW=ImageSX($im); # 取得原图宽度;
$srcH=ImageSY($im); # 取得原图高度;
$dstX=0;
$dstY=0;
if($File_height==0){
$File_height = $File_width/$srcW*$srcH;
}
if ($srcW*$File_height>$srcH*$File_width){
$fFile_height = round($srcH*$File_width/$srcW);
$dstY = floor(($File_height-$fFile_height)/2);
$fFile_width = $File_width;
}
else {
$fFile_width = round($srcW*$File_height/$srcH);
$dstX = floor(($File_width-$fFile_width)/2);
$fFile_height = $File_height;
}
$ni = ImageCreateTrueColor($File_width,$File_height);
$dstX = ($dstX<0)?0:$dstX;
$dstY = ($dstX<0)?0:$dstY;
$dstX = ($dstX>($File_width/2))?floor($File_width/2):$dstX;
$dstY = ($dstY>($File_height/2))?floor($File_height/s):$dstY;
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fFile_width,$fFile_height,$srcW,$srcH);
ImageJpeg($ni,$newFile,$rate); # 生成缩略图;
imagedestroy($im); # imagedestroy(resource) 释放image关联的内存
}
}
}
?>
您可能感兴趣的文章:
PHP图片上传类(多文件上传、缩略图、水印)
php 图片上传类的实现代码
php图片上传并保存到MySql数据库的实现代码
PHP图片上传的实例代码
PHP图片上传的简单例子
php图片加水印的小例子
PHP上传多文件、多图片的示例代码
PHP设置图片文件上传大小的方法
php上传多文件与多图片的实例代码
php 图片处理类(附实例)
php 上传图片的函数示例
PHP上传图片的简单例子(入门参考)
php 文件上传类与图片处理类的实现代码
PHP 图片文件上传的原理分析与代码
php 图片处理类(简单易用)
最新技术文章: