LAMP兄弟连原创视频教程(笔记五--文件上传下载,mysqli对象,session,cookie,date函数)

10.1 文件上传
文件上传步骤:
1.表单提交(注意上传时<form>必须method="post" enctype="multipart/form-data")
2.对文件的操作
<html>
<head>
<title>文件上传</titel>
</head>
<body>
<form action="todo.php",method="post" enctype="multipart/form-data">
 用户名:<input type="text" name="username"><br>
 密&nbsp;码:<input type="password" name="pass"><br>
 <input type="file" name="myfile">
 <input type="file" name="myfile2">
 <input type="hidden" name="MAX_FILE_SIZE" value="1000">
 <input type="submit" value="提交">
</form>
</body>
</html>

 

<?php
 print_r($_FILES);

 echo "上传文件名称:".$FILES["myfile"]["name"]."<br>";
 //标准的MIME类型 text/html/plain image/gif/jpeg/png audio/x-midi/x-wav video/quicktime/mpeg   

        //appliction/pdf/msword/vnd.ms-excel... multipart message
 echo "上传文件类型:".$FILES["myfile"]["type"]."<br>";
 echo "上传的临时文件:".$FILES["myfile"]["tmp_name"]."<br>";
 
 $copyto="uploads/".$_FILES["myfile"]["name"];
 
 if($_FILES["myfile"]["error"]>0)
 {
  switch($_FILES["myfile"]["error"])
  {
   case 1:
    echo "上传的文件超过了php.ini中upload_max_filesize这个选项设置的值 

    <br>";
    break;
   case 2:
    echo "上传的文件超过了html表单中的MAX_FILE_SIZE选项指定的值<br>";
    break;
   case 3:
    echo "文件只有部分被上传<br>";
    break;
   case 4:
    echo "没有文件被上传<br>";
    break;
  }
 
 }else
 { 
  
  if(is_uploaded_file($FILES["myfile"]["tmp_name"]))//判断是否是上传文件
  {
   if(move_uploaded_file($_FILES["myfile"]["name"],$copyto))//移动上传文件
   {
    echo "上传文件成功<br>";
   }else
   {
    echo "上传文件失败<br>";
   }
  }
 }
 echo "上传文件大小为:".$_FILES["myfile"]["size"]."b <br>";
 if($_FILES["myfile"]["type"]!="text/plain")
 {
  die("你上传的不是文本文件");
 }
?>

 

10.3 上传多个文件和文件下载处理

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="todo.php" method="post" enctype ="multipart/form-data" >
<input  name="myfile[]" type="file" />
<input  name="myfile[]" type="file" />
<input type="submit" name="Button1" value="Button" id="Button1" />
</form>

</body>
</html>
<?php
 $uploaddir="uploads/";
 for($i=0;$i<count($_FILES["myfile"]["name"]);$i++)
 {
  if($_FILES["myfile"]["size"][$i]>=40000)
  {
   echo $_FILES["myfile"]["name"][$i]."文件太大<br>";
   continue;
  }
  if($_FILES["myfile"]["type"][$i]!="text/plain")
  {
   echo $_FILES["myfile"]["name"][$i]."文件不是文本文件<br>";
   continue;
  }
  echo $_FILES["myfile"]["tmp_name"][$i];
  if(move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$uploaddir.$_FILES["myfile"]

  ["name"][$i]))
  {
   echo "上传成功<br>";
  }
 }

?>
文件下载
通知浏览器头信息
   1.文件下载的类型使用MIME类型表示
   2.下载文件的描述如给出文件名称等
   3.下载文件的长度以字节为单位
<?php
 //下载HTML见面
 $filename="one.html"; 
 header("Content-type:text/html");
 header('Content-Disposition: attachment; filename="download.html"');
 header("Content-Length:".filesize($filename));
 $fp=fopen($filename,"r");
 //readfile($filename);
 while(feof($fp)==false)
 {
  echo fread($fp,1024);
 }
 fclose($fp);
?>


mysqli
<?php
 $mysqli=new mysqli("localhost","root","","testDB");
 if(mysqli_conncet_errno())
 {
  echo "连接失败".mysqli_connect_error();
  exit;
 }

 //从结果集获取一条记录
 //mysqli_fetch_array()
 //mysqli_fetch_aasoc()
 //mysqli_fecth_row() 常用
  //mysqli_fetch_object()

 $result=$mysql->query("select * fromdemo");
/*
 while($row=$result->fetch_assoc!=false)
 {
  print_r($result);
 }
*/
 //$row=$result->fetch_array(MYSQL_ASSOC );
 
 $row=$result->fecth_object();
 echo $row->id;
 echo $row->name;
 $result=$mysqli->query("insert into demo(id,name) value(3,""xiao)");
 echo $result;
 $result=$mysqli->query("update demo set="xl" where id=1);
  echo $result;
 
?>

 

session与cookie

setcookie() 在使用应该函数时前面不能用任何输出
setCookie("username","xiao",time+60*60);
删除Cookie:setCookie("username")或者setCookie("username",'',time-60)
原形:bool setcookie(string name [,string value[,int expire [,string path [string domain [,bool

secure]]]]])
$_COOKIE["cookiename"]

<?php
 
 //必须先启动session,可以在php.ini设置自动启动(不建议)
 session_start();
 echo session_id();
 //删除session中的某个变量
 unset(_SESSION["username"]);//不能这样写unset($_SESSION);
 //清除seesion中的所有变量
 $_SESSION=array();
 //判断cookie是否有这个session_name变量
 if(isset($_COOKIE[session_name()]))
 {
  setCookie(session_name(),'',time()-60.'/');
 }
 
 //销毁和当前session有关的资料
 session_destroy();
 
?>


date函数
<?php
 echo date("Y-m-d M:i:s",time());
?>
getDate()函数
<?php
 $dates=getDate();
 print_r(dates);
?>
mktime()函数:返回一个unix时间戳

microtime()函数:返回当前Unix时间戳和微秒数
<?php
 class Timer{
  private $startTime;
  private $stopTime;
  function _construct(){
   $this_>startTime=0;
   $this->stopTime=0;
  }
  function start(){
   $this->startTime=microtime(true);
  }
  function stop(){
   $this->stopTime-microtime(true);
  }
  function spent(){
   return round($this->stopTime-$this->startTime,4);
  }
 
 }
 $timer=new Timer();
 $timer.start();
 usleep(1000);
 $timer.stop();
 echo $timer->spent();
?>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值