当前位置:首页 > 代码 > 正文

php代码实例(php网站开发实例教程代码)

admin 发布:2022-12-19 14:28 128


今天给各位分享php代码实例的知识,其中也会对php网站开发实例教程代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

php获取本机真实IP地址实例代码

本文实例为大家分享了php获取本机真实IP地址实例代码,供大家参考。

主要是获取操作系统为win2000/xp、win7的本机IP真实地址,和获取操作系统为linux类型的本机IP真实地址,具体内容如下

function

getLocalIP()

{

$preg

=

"/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";

//获取操作系统为win2000/xp、win7的本机IP真实地址

exec("ipconfig",

$out,

$stats);

if

(!empty($out))

{

foreach

($out

AS

$row)

{

if

(strstr($row,

"IP")

strstr($row,

":")

!strstr($row,

"IPv6"))

{

$tmpIp

=

explode(":",

$row);

if

(preg_match($preg,

trim($tmpIp[1])))

{

return

trim($tmpIp[1]);

}

}

}

}

//获取操作系统为linux类型的本机IP真实地址

exec("ifconfig",

$out,

$stats);

if

(!empty($out))

{

if

(isset($out[1])

strstr($out[1],

'addr:'))

{

$tmpArray

=

explode(":",

$out[1]);

$tmpIp

=

explode("

",

$tmpArray[1]);

if

(preg_match($preg,

trim($tmpIp[0])))

{

return

trim($tmpIp[0]);

}

}

}

return

'127.0.0.1';

}

以上就是本文的全部内容,希望对大家的学习有所帮助。

php目录操作实例代码

这篇文章主要介绍了php目录操作实例代码,需要的朋友可以参考下

代码如下:

?php

/**

*

listdir

*/

header("content-type:text/html;charset=utf-8");

$dirname

=

"./final/factapplication";

function

listdir($dirname)

{

$ds

=

opendir($dirname);

while

(false

!==

($file

=

readdir($ds)))

{

$path

=

$dirname.'/'.$file;

if

($file

!=

'.'

$file

!=

'..')

{

if

(is_dir($path))

{

listdir($path);

}

else

{

echo

$file."br";

}

}

}

closedir($ds);

}

listdir($dirname);

核心:递归的经典应用,以及文件和目录的基本操作。

代码如下:

?php

/**

*

copydir

*/

$srcdir

=

"../fileupload";

$dstdir

=

"b";

function

copydir($srcdir,

$dstdir)

{

mkdir($dstdir);

$ds

=

opendir($srcdir);

while

(false

!==

($file

=

readdir($ds)))

{

$path

=

$srcdir."/".$file;

$dstpath

=

$dstdir."/".$file;

if

($file

!=

"."

$file

!=

"..")

{

if

(is_dir($path))

{

copydir($path,

$dstpath);

}

else

{

copy($path,

$dstpath);

}

}

}

closedir($ds);

}

copydir($srcdir,

$dstdir);

核心:copy函数。

代码如下:

?php

/**

*

deldir

*/

$dirname

=

'a';

function

deldir($dirname)

{

$ds

=

opendir($dirname);

while

(false

!==

($file

=

readdir($ds)))

{

$path

=

$dirname.'/'.$file;

if($file

!=

'.'

$file

!=

'..')

{

if

(is_dir($path))

{

deldir($path);

}

else

{

unlink($path);

}

}

}

closedir($ds);

return

rmdir($dirname);

}

deldir($dirname);

核心:注意unlink删除的是带path的file。

代码如下:

?php

/**

*

dirsize

*/

$dirname

=

"a";

function

dirsize($dirname)

{

static

$tot;

$ds

=

opendir($dirname);

while

(false

!==

($file

=

readdir($ds)))

{

$path

=

$dirname.'/'.$file;

if

($file

!=

'.'

$file

!=

'..')

{

if(is_dir($path))

{

dirsize($path);

}

else

{

$tot

=

$tot

+

filesize($path);

}

}

}

return

$tot;

closedir($ds);

}

echo

dirsize($dirname);

核心:通过判断$tot在哪里返回,理解递归函数。

php 截取utf-8格式的字符串实例代码

php

截取utf-8格式的字符串

php中,我们经常需要截取字符串。英文字符占用一个字节,中文字符占用两个字节,但中文字符占用两个字节是相对于GBK编码而言但是在时下国际流行的UTF8编码中,一个中文字符占用3个字节。本文章向大家介绍一个php

截取utf-8格式字符串的函数。

举例说明:

function

truncate_utf8_string($string,

$length,

$etc

=

'...')

{

$result

=

'';

$string

=

html_entity_decode

(

trim

(

strip_tags

(

$string

)

),

ENT_QUOTES,

'UTF-8'

);

$strlen

=

strlen

(

$string

);

for($i

=

0;

(($i

$strlen)

($length

0));

$i

++)

{

if

($number

=

strpos

(

str_pad

(

decbin

(

ord

(

substr

(

$string,

$i,

1

)

)

),

8,

'0',

STR_PAD_LEFT

),

'0'

))

{

if

($length

1.0)

{

break;

}

$result

.=

substr

(

$string,

$i,

$number

);

$length

-=

1.0;

$i

+=

$number

-

1;

}

else

{

$result

.=

substr

(

$string,

$i,

1

);

$length

-=

0.5;

}

}

$result

=

htmlspecialchars

(

$result,

ENT_QUOTES,

'UTF-8'

);

if

($i

$strlen)

{

$result

.=

$etc;

}

return

$result;

}

如果需要截取utf-8格式的字符串,直接调用这个函数即可。

?php

$str="如果需要截取utf-8格式的字符串,直接调用这个函数即可。";

echo

truncate_utf8_string($str,10);//输出结果:如果需要截取utf-8格...

?

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

php存储过程调用实例代码

复制代码

代码如下:

//比如要调用的存储过程为gxtj(a,b)

$db=new

mysqli("localhost","ssss","aaaaa","bbbb");

mysqli_query($db,"SET

NAMES

utf8");

$result=$db-query("call

gxtj($year,$jd)");

//

gxtj是mysql的存储过程名称

[color=gray][/color]

while(

$row

=

$result-fetch_array(MYSQLI_ASSOC))

//完成从返回结果集中取出一行

{

while

($key=key($row)){

//依次取得字段名

$value=current($row);

//依次取得字段值

}

}

实例一:无参的存储过程

复制代码

代码如下:

$conn

=

mysql_connect('localhost','root','root')

or

die

("数据连接错误!!!");

mysql_select_db('test',$conn);

$sql

=

"

create

procedure

myproce()

begin

INSERT

INTO

user

(id,

username,

sex)

VALUES

(NULL,

's',

'0');

end;

";

mysql_query($sql);//创建一个myproce的存储过程

$sql

=

"call

test.myproce();";

mysql_query($sql);//调用myproce的存储过程,则数据库中将增加一条新记录。

php删除文件夹操作函数和几种方式实例代码汇总

先看一下代码

复制代码 代码如下:

?

function deldir($dir) {

//先删除目录下的文件:

$dh=opendir($dir);

while ($file=readdir($dh)) {

if($file!="." $file!="..") {

$fullpath=$dir."/".$file;

if(!is_dir($fullpath)) {

unlink($fullpath);

} else {

deldir($fullpath);

}

}

}

closedir($dh);

//删除当前文件夹:

if(rmdir($dir)) {

return true;

} else {

return false;

}

}

?

unlink() 函数用于删除文件。若成功,则返回 true,失败则返回 false。rmdir() 函数用于删除空的目录。它尝试删除 dir 所指定的目录。 该目录必须是空的,而且要有相应的权限。

一个实例:删除某个文件夹下的所有".svn"文件夹(包括其内容也要被删除)。

复制代码 代码如下:

?php

function delsvn($dir) {

$dh=opendir($dir);

//找出所有".svn" 的文件夹:

while ($file=readdir($dh)) {

if($file!="." $file!="..") {

$fullpath=$dir."/".$file;

if(is_dir($fullpath)) {

if($file==".svn"){

delsvndir($fullpath);

}else{

delsvn($fullpath);

}

}

}

}

closedir($dh);

}

function delsvndir($svndir){

//先删除目录下的文件:

$dh=opendir($svndir);

while($file=readdir($dh)){

if($file!="."$file!=".."){

$fullpath=$svndir."/".$file;

if(is_dir($fullpath)){

delsvndir($fullpath);

}else{

unlink($fullpath);

}

}

}

closedir($dh);

//删除目录文件夹

if(rmdir($svndir)){

return true;

}else{

return false;

}

}

$dir=dirname(__FILE__);

//echo $dir;

delsvn($dir);

?

PHP等比例压缩图片的实例代码

具体代码如下所示:

/**

*

desription

压缩图片

*

@param

sting

$imgsrc

图片路径

*

@param

string

$imgdst

压缩后保存路径

*/

public

function

compressedImage($imgsrc,

$imgdst)

{

list($width,

$height,

$type)

=

getimagesize($imgsrc);

$new_width

=

$width;//压缩后的图片宽

$new_height

=

$height;//压缩后的图片高

if($width

=

600){

$per

=

600

/

$width;//计算比例

$new_width

=

$width

*

$per;

$new_height

=

$height

*

$per;

}

switch

($type)

{

case

1:

$giftype

=

check_gifcartoon($imgsrc);

if

($giftype)

{

header('Content-Type:image/gif');

$image_wp

=

imagecreatetruecolor($new_width,

$new_height);

$image

=

imagecreatefromgif($imgsrc);

imagecopyresampled($image_wp,

$image,

0,

0,

0,

0,

$new_width,

$new_height,

$width,

$height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp,

$imgdst,

90);

imagedestroy($image_wp);

imagedestroy($image);

}

break;

case

2:

header('Content-Type:image/jpeg');

$image_wp

=

imagecreatetruecolor($new_width,

$new_height);

$image

=

imagecreatefromjpeg($imgsrc);

imagecopyresampled($image_wp,

$image,

0,

0,

0,

0,

$new_width,

$new_height,

$width,

$height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp,

$imgdst,

90);

imagedestroy($image_wp);

imagedestroy($image);

break;

case

3:

header('Content-Type:image/png');

$image_wp

=

imagecreatetruecolor($new_width,

$new_height);

$image

=

imagecreatefrompng($imgsrc);

imagecopyresampled($image_wp,

$image,

0,

0,

0,

0,

$new_width,

$new_height,

$width,

$height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp,

$imgdst,

90);

imagedestroy($image_wp);

imagedestroy($image);

break;

}

}

总结

以上所述是小编给大家介绍的PHP等比例压缩图片的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:php中10个不同等级压缩优化图片操作示例PHP

实现等比压缩图片尺寸和大小实例代码php

gd等比例缩放压缩图片函数基于PHP实现等比压缩图片大小php上传图片并压缩的实现方法PHP实现图片上传并压缩PHP实现图片压缩的两则实例php使用imagick模块实现图片缩放、裁剪、压缩示例

关于php代码实例和php网站开发实例教程代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/9421.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载