php程序设计经典300例,第21-22例

第21例
check.php页面:


<?php
//图片上传到服务器

function fileext($filename)
  {
    return substr(strrchr($filename,‘.‘),1);  //获取扩展名
    
  }
function random($length)
  {  //生成随机文件名函数
    $hash=‘CR-‘;
    $chars=‘abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz‘;
    $max=strlen($chars)-1;
    mt_srand((double)microtime()*1000000);
      for($i=0;$i<$length;$i++)
       {
        $hash.=$chars(mt_rand(0,$max));
       }
       return $hash;
  }

?>

form.php页面:


<form action="upload.php" method="post" enctype="multipart/form-data"><!--定义method属性-->
      <table border="0" cellspacing="0" cellpadding="0" align="center" width="300px">
           <tr>
               <td width="55" height="20" align="center">
                  <input type="hidden" name="MAX_FILE_SIZE" value="2000000"/>文件:
               </td>
               <td height="16">
                   <input type="file" name="file" value="浏览"/>
               </td>
           </tr>
           <tr>
               <td align="center" colspan="2"><br />
                    <input type="submit" name="B1" value="上传"/>
               </td>
           </tr>
      </table>
</form>


upload.php页面:
<?php
  
   include("check.php");
   $uploaddir="./files?";
   $type=array("jpg","gif","bmp","jpeg","pan");//设置允许上传文件的类型
   if(!in_array(strtolower(fileext($_FILES[‘file‘][‘name‘])),$type))//判断上传文件的类型是否在范围内
     {
        $text=implode(",",$type);
        echo "您只能上传以下类型文件:",$text,"<br/>";
     }else
       {
        $filename=explode(".",$_FILES[‘file‘][‘name‘]);//获取文件名
        do{
            $filename[0]=random(10);
            $name=implode(".",$filename);
            $uploadfile=$upload.$name;
          }
        while(file_exists($uploadfile));
          if(is_uploaded_file($_FILES[‘file‘][‘tmp_name‘]))
            {
                if(move_uploaded_file($_FILES[‘file‘][‘tmp_name‘],$uploadfile))
                 {
                    echo "<center>您的文件已经上传完毕 上传图片预览:</center><br/><center><img src=‘$uploadfile‘></center";
                    echo "<br/><center><a href=‘javascript:history.go(-1)‘>继续上传</a></center>";
                 }else
                   {
                    echo "上传失败";
                   }
            }
       }
?>


第22例


<!--在外部文件中读取并显示用户协议-->

<table width="100" border="0" cellpadding="1" cellspacing="1" bgcolor="#cccccc">
   <tr>
        <td bgcolor="#ffffff">
           <!--写文本内容要加载区-->
           
        </td>
   </tr>
</table>
<form action="zhuce.php" method="post" name="register" id="form">
      <input type="hidden" name="action" id="action" value="agree"/>
      <input type="submit" name="submit" value="同意"/>
</form>
<form action="" method="post" name="form" id="from">
      <a href="index.php">
           <input type="reset" name="reset1" id="reset1" value="不同意"/>
      </a>
</form>

本文出自 “钟泽锋” 博客,请务必保留此出处http://8281493.blog.51cto.com/8271493/1623529

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。