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

文件上传,java文件上传

WBOY
WBOY原創
2016-06-13 08:39:091394瀏覽

文件上传,java文件上传

文件上传分为两个步骤

1客户端用户上传;

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

<span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file"</span> <span>/></span>

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>

 

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>

 

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

                  

此时要进行数据验证

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>


 

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

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

eg:

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

 

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

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

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn