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>
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>
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>
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
Data verification is required at this time
<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>
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']);
Note: tmp_name, the temporary directory where files are uploaded to the server,
'name', the local file name of the file on the client.