Home  >  Article  >  Backend Development  >  PHP文件上传操作

PHP文件上传操作

WBOY
WBOYOriginal
2016-06-23 13:38:49974browse

PHP文件上传主要两个步骤:

1.首先前端html写好文件表单上传网页

2.在前端点提交时,web服务器php脚本通过超全局变量$_FILES和一个move_uploaded_file函数搞定

前端html如下:

    <meta charset="utf-8">    <title>文件上传</title>    
注:表单上传文件时,method必须用post,且须声明是enctype="multipart/form-data"

2.服务器php脚本upload.php代码如下:

<?phpif (move_uploaded_file($_FILES['uploadpic']['tmp_name'], './fileupload/'.$_FILES['uploadpic']['name'])){    echo "ok";    }else {    echo "fail";}
注:可以用print_r($_FILES)打印查看超全局变量里面放的内容,可以看到文件的相关信息都放在这个变量里面;如

array (  'uploadpic' =>   array (    'name' => '1客栈首页.jpg',    'type' => 'image/jpeg',    'tmp_name' => 'C:\\Windows\\Temp\\php3F1C.tmp',    'error' => 0,    'size' => 1706919,  ),)ok
从变量的数组里面可以知道上传的文件名,文件类型、web服务器临时存放图片的位置(如果要更改临时存放路径,可以去php.ini里面更改),错误信息和文件大小(限制文件上传大小,也可以到php.ini里修改)。




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