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

php代码实例(php代码示例)

admin 发布:2022-12-19 22:45 150


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

本文目录一览:

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等比例压缩图片的实例代码

具体代码如下所示:

/**

*

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删除文件夹操作函数和几种方式实例代码汇总

先看一下代码

复制代码 代码如下:

?

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代码实例和php代码示例的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

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

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


取消回复欢迎 发表评论:

分享到

温馨提示

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

联系我们反馈

立即下载