Home > php教程 > php手册 > 文件上传,java文件上传

文件上传,java文件上传

WBOY
Release: 2016-06-13 08:39:09
Original
1389 people have browsed it

文件上传,java文件上传

文件上传分为两个步骤

1客户端用户上传;

a)需要一个表单域,用来注入要上传的文件,

<span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file"</span> <span>/></span>
Copy after login

b)用户选择所需要上传的文件

c)用户点击,将文件发送给服务器

2服务器接收

form表单使用POST提交方式。

*注意:GET,POST不能提交二进制文件,但是POST提交增加一个属性便可以完成。

<span><</span><span>form </span><span>action</span><span>="file.php"</span><span> method</span><span>="POST"</span><span>  enctype</span><span>="multipart/form-data"</span><span>></span>
Copy after login

html部分:

html部分:
DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传title>
head>
<body>
    <span><</span><span>form </span><span>action</span><span>="file.php"</span><span> method</span><span>="POST"</span><span>  enctype</span><span>="multipart/form-data"</span><span>></span>
        <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file"</span> <span>/></span>
        <input type="submit" value="提交"/>
    form>
body>
Copy after login

php部分

var_dump($_FILES) 时发现得到的是一个数组

<span>array</span>
  'file' => 
    <span>array</span>
      'name' => <span>string</span> '4.jpg' (length=5<span>)
      </span>'type' => <span>string</span> 'image/jpeg' (length=10<span>)
      </span>'tmp_name' => <span>string</span> 'F:\wamp\tmp\php904D.tmp' (length=23<span>)
      </span>'error' => int 0
      'size' => int 273665
Copy after login

此时要进行数据验证

a)根据二维数组找到error点;

<span>switch</span>(<span>$_FILES</span>['file']['error'<span>]){
    </span><span>case</span> '0':
        <span>echo</span> "上传成功"<span>;
        </span><span>break</span><span>;
    </span><span>case</span> '1':
    <span>case</span> '2':
        <span>header</span>('Refresh:3;url=file.html'<span>);
        </span><span>echo</span> "文件大小超过服务器限制"<span>;
        </span><span>break</span><span>;
    </span><span>case</span> '3':
        <span>header</span>('Refresh:3;url=file.html'<span>);
        </span><span>echo</span> "文件只有部分上传成功"<span>;
        </span><span>break</span><span>;
    </span><span>case</span> '4':
        <span>header</span>('Refresh:3;url=file.html'<span>);
        </span><span>echo</span> "未选中文件,请重新选择文件并提交"<span>;
        </span><span>break</span><span>;
    </span><span>default</span>:
        <span>echo</span> "文件上传失败"<span>;
}</span>
Copy after login


b)文件上传成功后,放到了指定的临时目录中,此时需要更改到期待的文件中,

php有一个函数可以完成,move_uploaded_file()

eg:

<span>move_uploaded_file</span>(<span>$_FILES</span>['file']['tmp_name'], './cy1/' . <span>$_FILES</span>['file']['name']);
Copy after login

 

注:tmp_name,文件上传到服务器的临时目录,

'name',文件在客户端本地的文件名字。

 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template