php代码小实例

时间:2024-01-15 18:56:44

php多图上传

<html>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <head>
        <title>多文件上传</title>
    </head>
    <body>
    <form accept="" method="post" enctype="multipart/form-data">
    <input  type="file" name="img[]" /><br />
    <input  type="file" name="img[]" /><br />
    <input  type="file" name="img[]" /><br />
    <input  type="file" name="img[]" /><br />
    <input  type="file" name="img[]" /><br />
    <input  type="file" name="img[]" /><br />
    <input  type="submit" name="s" /><br />
    </form>

<?php
    //上传文件信息
    $img = $_FILES['img'];
    if ($img)
    {
        //文件存放目录,和本php文件同级
        $dir = dirname(__file__);
        $i = 0;
        foreach ($img['tmp_name'] as $value)
        {
            $filename = $img['name'][$i];
            if ($value)
            {
         		$savepath="$dir\\$filename";
                $state = move_uploaded_file($value, $savepath);
                //如果上传成功,预览
                if($state)
                {
           	 		echo "<img src='$filename' alt='$filename' /> ";
                }
            }
            $i++;
        }
    }
?>
    </body>
</html>

  清除目录下的文件

<?
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;
  }
}
?>

  实现相关文章的功能实现

一般做内容网站,需要在每一篇文章出现与该文章相关的文章列表。多数人使用的方法大概是:建立一个关键词列表,判断每篇文章包含有那些关键词,最后根据关键词找出与某篇文章最相关的文章。对于内容比较复杂的网站,确定关键列表词显然比较麻烦。

后来我查阅了一些php函数,感觉similar_text(php4,php5)函数能够十分方便的达到我的要求。这个思路是:从文章列表中取出所有的文章标题,将所有的文章标题都同当前标题对比,将对比结果生成一个数组,按照相似度的大小由大到标题,利用similar_text将这些文章标题同原文章标题做对比,按标题的相似程度重新排列标题,就得到了与原文章相似的文章列表。

这个思路用到的关键函数是:

1 int similar_text ( string $first, string $second [, float $percent] )

它返回的是两个字根串的相同字节数。

按照这个思路,我们建立如下的函数,这个函数的功能是把$arr_title数组按照同$title相似的的顺序重新排列数组。

<?php
$demo_title = "简明现代魔法";
$demo_arr_title = array("简单易懂的现代魔法","简单明了的现代魔法","简明扼要的古代魔法","不简单的现代魔法","很难懂的现代魔法");
$new_array = getSimilar($demo_title,$demo_arr_title);
//print_r($new_array);
echo "与[$demo_title]最相关的前三个文章是:<br/>";
for($j=0; $j<=2; $j++)
{
	echo ($j+1).":".$new_array[$j]."<br/>";
}

//$title当前标题,$arrayTitle为需要查找的数组
function getSimilar($title,$arr_title)
{
	$arr_len = count($arr_title);
	for($i=0; $i<=($arr_len-1); $i++)
	{
		//取得两个字符串相似的字节数
		$arr_similar[$i] = similar_text($arr_title[$i],$title);
	}
	arsort($arr_similar);	//按照相似的字节数由高到低排序
	reset($arr_similar);	//将指针移到数组的第一单元
	$index = 0;
	foreach($arr_similar as $old_index=>$similar)
	{
		$new_title_array[$index] = $arr_title[$old_index];
		$index++;
	}
	return $new_title_array;
}
?>

  

程序运行结果:

与[简明现代魔法]最相关的前三个文章是:
1:简单明了的现代魔法
2:简单易懂的现代魔法
3:简明扼要的古代魔法

  php图片的裁剪

<?php
list($src_w,$src_h)=getimagesize($src_img);  // 获取原图尺寸
$dst_scale = $dst_h/$dst_w; //目标图像长宽比
$src_scale = $src_h/$src_w; // 原图长宽比

if($src_scale>=$dst_scale)
{
	// 过高
	$w = intval($src_w);
	$h = intval($dst_scale*$w);
	$x = 0;
	$y = ($src_h - $h)/3;
}
else
{
// 过宽
	$h = intval($src_h);
	$w = intval($h/$dst_scale);
	$x = ($src_w - $w)/2;
	$y = 0;
}
// 剪裁
$source=imagecreatefromjpeg($src_img);
$croped=imagecreatetruecolor($w, $h);
imagecopy($croped,$source,0,0,$x,$y,$src_w,$src_h);
// 缩放
$scale = $dst_w/$w;
$target = imagecreatetruecolor($dst_w, $dst_h);
$final_w = intval($w*$scale);
$final_h = intval($h*$scale);
imagecopysampled($target,$croped,0,0,0,0,$final_w,$final_h,$w,$h);
// 保存
$timestamp = time();
imagejpeg($target, "$timestamp.jpg");
imagedestroy($target);
?>

  大并发下的优化处理

1.redis

2.使用加速Xcache

3.使用文件锁

至于反射 我们就可以利用这个特性进行架构的开发

地址:http://php.net/manual/zh/reflectionclass.getproperties.php