How to process form upload files in php

小云云
Release: 2023-03-22 13:02:01
Original
3873 people have browsed it

When using a form to upload files, the submission method of the form must be post submission. The value of the input control type attribute of the uploaded file should be file. The most important thing is that the form tag needs to add an enctype="multipart/form-data" properties. This article mainly shares with you the method of processing form upload files in PHP. I hope it can help you.

html code:

  1. <!DOCTYPE html>  
    <html lang="en">  
    <head>  
        <meta charset="UTF-8">  
        <title>上传</title>  
    </head>  
    <body>  
        <form action="handle.php" method="post" enctype="multipart/form-data">  
            <input type="file" name="file1">  
            <br/>  
            <input type="submit" value="上传">  
        </form>  
    </body>  
    </html>
    Copy after login

php code:

  1. <?php  
        /* 
         *用$_FILES接收通过表单上传的文件数据  
         *$_FILES[&#39;file1&#39;]中的file1对应表单中上传文件的input标签中的name值 
         *$_FILES[&#39;file1&#39;]返回的是一个数组: 
         $_FILES[&#39;file1&#39;][&#39;name&#39;] 显示上传文件的原名称。 
         $_FILES[&#39;file1&#39;][&#39;type&#39;] 文件的 MIME 类型,例如"image/gif"、"image/png"。 
         $_FILES[&#39;file1&#39;][&#39;size&#39;] 已上传文件的大小,单位为字节。 
         $_FILES[&#39;file1&#39;][&#39;tmp_name&#39;] 储存的临时文件名和临时储存的路径。 
         $_FILES[&#39;file1&#39;][&#39;error&#39;] 和该文件上传相关的错误代码。 
         =0; 没有错误发生,文件上传成功。  
         =1; 上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。  
         =2; 上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。  
         =3; 文件只有部分被上传。  
         =4; 没有文件被上传。  
         =5; 上传文件大小为0.  
         */  
        $file=$_FILES[&#39;file1&#39;];  
      
        //给文件重命名,尽量不要起中文名字,如下以时间戳+随机数重命名  
        $filename=time().rand(0,1000);  
      
        //获取文件的后缀,pathinfo()会以数组的形式返回一个文件的路径信息,其中extension元素则是文件的后缀名  
        $ext=pathinfo($_FILES[&#39;file1&#39;][&#39;name&#39;])[&#39;extension&#39;];  
      
        //最终文件名为  
        $filename=$filename.&#39;.&#39;.$ext;  
      
        //设置文件上传到服务器后存放的位置,move_uploaded_file()会将文件移动到新位置,第一个参数是要移动的文件,第二个参数是移动到哪里。我在这里是放到和本php文件同一目录下,注意需要将新的文件名连接进去。  
        if (move_uploaded_file($file[&#39;tmp_name&#39;], &#39;./&#39;.$filename)) {  
            echo "success";  
         } else{  
            echo "error";  
         }    
     ?>
    Copy after login

Related recommendations:

php How to handle forms?

The above is the detailed content of How to process form upload files in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template