文件上传模块

    今天写了一个很简单的上传模块,是参考《php与mysql web》这本书上写的还有待完善,发现好多函数都不记得了,看来新手还是要多练习呀。收获了以下一些知识点:

获取变量的数据类型

gettype($a);

1.      basename函数的作用:

<?php

  $path=”test/home.php”;

 

  echo basename($path);

  echo basename($path,”.php”);

?>

 

输出: home.php

       home

2.      move_upload_file函数的作用

move_upload_file(file,newpath);

将file文件移到新的位置newpath,若成功则返回true,否则返回false。

 

 程序代码:

  upload.php

<html>

  <head>

     <meta http-equiv="content-type"content="text/html;charset=utf-8"/>

           <title>文件上传</title>

  </head>

  <form action="uploadprocess.php" method="POST"enctype="multipart/form-data" />

      <input type="hidden" name="MAX_FILE_SIZE"value="300000000"/>

            上传:<inputname="userfile" type="file" id="userfile"/>

            <input name="sub"type="submit" value="上传"/>

  </form>

</html>

 

在<form>标记中,必须设置属性enctype=multipart/form-data,这样,服务器就可以知道上传文件带有常规信息

 

uploadprocess.php

<html>

<head>

   <metahttp-equiv="content-type" content="text/html;charset=utf-8"/>

</head>

  <?php

    if($_FILES['userfile']['error']>0){

           switch($_FILES['userfile']['error']){

                   case 1: echo '上传文件超出了php.ini中的配置';

                   break;

                   case 2: echo '上传文件超过了MAX_FILE_SIZE的值';

                   break;

                   case 3: echo '文件只上传一部分';

                   break;

                   case 4: echo '没有文件被上传';

                   break;

                   case 6: echo '没有找到临时文件夹';

                   break;

                   case 7: echo '文件写入失败';

                   break;

                   default: echo '上传失败';

                }

        }

 

        if($_FILES['userfile']['type']!='text/plain'){

           echo 'Problem:file is not plain text';

                exit;

        }

        $upfile='./uploads/'.$_FILES['userfile']['name'];

       // echo $upfile;

        $name=$_FILES['userfile']['name'];

 

        if(is_uploaded_file($_FILES['userfile']['tmp_name'])){

          if(!move_uploaded_file($_FILES['userfile']['tmp_name'],$upfile)){

                   echo '不能移动到指定的目录';

                   exit();

 

                }

        }else{

           echo '可能文件不存在';

                exit();

        }

 

        echo '上传成功';

 

        $contents=file_get_contents($upfile);//$contents是字符串的数据类型

         //echo gettype($contents);

        $contents=strip_tags($contents); 

 

        file_put_contents($_FILES['userfile']['name'],$contents);

 

        echo '上传文件的内容为:<br/>';

        echo nl2br($contents);

  ?>

</html>

$_FILES[‘userfile’][‘name’]保存的是用户本地文件的名称。

$_FILES[‘userfile’][‘type’]保存上传文件的类型

$_FILES[‘userfile’][‘size’]保存文件上传的大小

$_FILES[‘userfile’][‘tmp_name’]保存临时文件的名称

  

$_FILES[‘userfile’][‘error’]错误信息:

0没有问题

1上传文件超过了php.ini中的设置大小

2上传文件超过了html中MAX_FILE_SIZE的大小

3文件只上传一部分

4文件没有被上传

6文件没有被上传

7.文件写入失败

 

Is_uploaded_file($file)判断文件是不是通过HTTP POST方式上传的,若是,则返回true,否则返回false;

 

move_uploaded_file($_FILES[‘userfile’][‘name’],$path);的作用是把$_FILES[‘userfile’][‘name’]移动到$path路径下.

 

$contents=file_get_contents($upfile);

file_get_contents() 是把整个文件读入到一个字符串中。

 

strip_tags(string,allow) 函数剥去 HTML、XML 以及 PHP 的标签。但是allow规定允许的标签不会删除

<?php

  echo strip_tags("hello<b>world</b>")."<br/>";

  echo strip_tags("<b><i>helloworld</i></b>","<b>");

?>

输出结果为:

 helloworld
hello world


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值