Home > Backend Development > PHP Tutorial > File upload, java file upload_PHP tutorial

File upload, java file upload_PHP tutorial

WBOY
Release: 2016-07-12 08:51:43
Original
974 people have browsed it

File upload, java file upload

File upload is divided into two steps

1, Uploaded by client users;

a) A form field is required to inject the file to be uploaded,

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

b) The user selects the file to be uploaded

c) The user clicks to send the file to the server

2, server receives

Form forms use POST submission method.

*Note: GET and POST cannot submit binary files, but POST submission can be completed by adding an attribute.

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

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 part

When var_dump($_FILES) was used, it was found that what was obtained was an array

<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

                                                  

Data verification is required at this time

a) Find the error point based on the two-dimensional array;

<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) After the file is successfully uploaded, it is placed in the designated temporary directory. At this time, it needs to be changed to the expected file,

php has a function that can be done,

move_uploaded_file()

eg:

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

Note: tmp_name, the temporary directory where files are uploaded to the server,

'name', the local file name of the file on the client.

http://www.bkjia.com/PHPjc/1128377.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1128377.htmlTechArticleFile upload, java file upload File upload is divided into two steps 1, client user upload; a) requires a Form field, used to inject the file to be uploaded, input type ="file" 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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template