zoukankan      html  css  js  c++  java
  • php处理文件上传

    注意点:

    1、<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">处理文件上传的时候,如果一个表单里面有文件域的时候,mothod="post",enctype="multipart/form-data',这里必须这样设置

    2、用$_FILES超全局成员来接受文件

    3、move_uploaded_file(sourcename,targetname);移动的目标路径中文件夹到目的文件夹

    4、文件上传的大小限制,和post请求体的大小限制

    这个需要修改php.ini文件upload_max_filesize=  ;  和post_max_size= ;

    <?php
    function upload(){
    
    $_FILES;//获取接受的文件
    if(empty($_FILES['avatar'])){
    	$GLOBALS['message'] = '请选择文件';
    	return ;
    }
    $avatar=$_FILES['avatar'];
    
    if($avatar['error']!==0){//如果没有错误时显示0
       $GLOBALS['message'] = '有文件上传错误';
       return ;
    }
    //为了便于理解,我们可以将在客户端返回的内容写在这里
    // $avatar => array(5) {
      //   ["name"]=>      
      //   string(11) "icon-02.png"
      //   ["type"]=>
      //   string(9) "image/png"
      //   ["tmp_name"]=>
      //   string(27) "C:WindowsTempphp1138.tmp"
      //   ["error"]=>
      //   int(0)
      //   ["size"]=>
      //   int(4398)
      // }
    
    //上面两步都没有错误,说明,文件文件上传成功
    echo $GLOBALS['message'] ="上传成功";
    
    //在服务端并没有显示这个图片的路径,我们必须把上传文件时那个暂时存放的路径指向一个能在文件根目录显示的路径
    
    //源文件
    $source=$avatar["tmp_name"];
    //目的文件
    $target=mkdir('load').$avatar["name"];
    //移动
    $moved = move_uploaded_file($source, $target);
    if(!$moved){
    	$GLOBALS['message'] = "上传失败";
    	return;
    }
    
    }
    
    if($_SERVER['REQUEST_METHOD']=='POST'){
    	upload();
    }
    ?>
    
    
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Document</title>
    </head>
    <body>
    	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    		<input type="file" name="avatar">
    		<button>提交</button>
    	</form>
    </body>
    </html>
    虽然现在走得很慢,但不会一直这么慢
  • 相关阅读:
    汽车租赁系统
    两种设计模式(2)==>>"单例"
    两种设计模式(1)==>>“简单工厂”
    面向对象设计的七大原则
    springboot中的mybatis是如果使用pagehelper的
    vue
    vue
    vue
    idea快捷键
    idea部署tomcat项目时,在项目里打断点不能拦截
  • 原文地址:https://www.cnblogs.com/xxm980617/p/10460121.html
Copyright © 2011-2022 走看看