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

文件上传

WBOY
WBOYOriginal
2016-05-30 16:59:421222browse

文件上传分为两个步骤

1客户端用户上传;

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

<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="file"</span> <span style="color: #0000ff;">/></span></span>

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

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

 

2服务器接收

form表单使用POST提交方式。

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

<span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="file.php"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="POST"</span><span style="color: #ff0000;">  enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span></span>

 

html部分:

<span style="color: #000000;">html部分:
</span><span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">html </span><span style="color: #ff0000;">lang</span><span style="color: #0000ff;">="en"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">charset</span><span style="color: #0000ff;">="UTF-8"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>文件上传<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="file.php"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="POST"</span><span style="color: #ff0000;">  enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="file"</span> <span style="color: #0000ff;">/></span>
        <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="提交"</span><span style="color: #0000ff;">/></span>
    <span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span>

 

php部分

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

<span style="color: #0000ff;">array</span>
  'file' => 
    <span style="color: #0000ff;">array</span>
      'name' => <span style="color: #0000ff;">string</span> '4.jpg' (length=5<span style="color: #000000;">)
      </span>'type' => <span style="color: #0000ff;">string</span> 'image/jpeg' (length=10<span style="color: #000000;">)
      </span>'tmp_name' => <span style="color: #0000ff;">string</span> 'F:\wamp\tmp\php904D.tmp' (length=23<span style="color: #000000;">)
      </span>'error' => int 0
      'size' => int 273665

                  

此时要进行数据验证

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

 

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


 

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

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

eg:

<span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>['file']['tmp_name'], './cy1/' . <span style="color: #800080;">$_FILES</span>['file']['name']);

 

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

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

 

Statement:
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