formenctypeformenctype=multipart/form-datamethod=postname=upform>
只有其值为multipart/form-data才能保证以正确的编码方式上传文件。input标签type属性中的file
inputnameinputname=upfiletype=file>
另一个系统函数是$_files,$_files['myfile']['name']客户端文件的原名称、$_files['myfile']['type']文件的mime类型,例如image/gif、$_files['myfile']['size']已上传文件的大小,单位为字节、$_files['myfile']['tmp_name']储存的临时文件名,一般是系统默认、$_files['myfile']['error']该文件上传相关的错误代码。这个函数将上传文件的信息分割成数组形式保存在不同的数组元素中,例如,文件名的值存储在$_files['myfile']['name']中。下面附上自己写的简单的php上传文件代码:php上传文件代码类saveupload.php
php if(is_uploaded_file($_files['upfile']['tmp_name'])){ $upfile=$_files[upfile];//如果已经选定了要上传的文件,将其索引保存在$upfile中 //分别去上传文件的名字,类型等 $name=$upfile[name]; $type=$upfile[type]; $size=$upfile[size]; $tmp_name=$upfile[tmp_name]; $error=$upfile[error]; //设定上传文件类型 switch($type){ case'image/pjpeg': $ok=1; break; case'image/jpeg': $ok=1; break; case'image/png': $ok=1; break; case'image/gif': $ok=1; break; } //如果文件类型合法并且$error返回值为0,说明上传成功 if($ok&&$error=='0'){ move_uploaded_file($tmp_name,'up/'.$name);//将保存在缓存的文件移动到指定目录下 echo上传成功; } } ?>
php上传文件代码上传页面upload.php
-//w3c//dtdxhtml1.0transitional//enhttp://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd> htmlxmlnshtmlxmlns=http://www.w3.org/1999/xhtml> head> metahttp-equivmetahttp-equiv=content-typecontent=text/html;charset=utf-8/> title>uploadtitle> styletypestyletype=text/css>
