如何从表单中获得上传文件属性

问题:

我做了一个表单index.html:

 1<form action="process.php" enctype="multipart/form-data" method="post" name="form1">
 2<table>
 3<tr>
 4<td><input name="pic" size="30" type="file"/></td>
 5</tr>
 6<tr>
 7<td>
 8<input id="Submit" name="Submit" type="submit" value="Send"/>   
 9       
10<input name="Submit2" type="reset" value="Reset"/>
11</td>
12</tr>
13</table>
14</form>

提交后的处理页process.php:

1   
2echo filetype($pic);   
3echo basename($pic);   

我在file表单域打开一个图片文件,想输出文件名和类型,但是为什么总是不能输出,结果是输出文件名为:"c:\windows\temp\php2",而文件类型为:"file"
为什么不是我的文件所在的绝对路径,和我想得到的"jpeg"类型呢,请高手帮帮我.

---------------------------------------------------------------

 1   
 2include ("../../lib/setting.php");PangRight($SAS_SESSION[userlogin][spwd]);   
 3if (count($HTTP_POST_VARS)>0){   
 4  
 5if(!empty($mydocfile)){   
 6//echo "cp ".$attachfile." ".$mydocfile.$filename;   
 7copy($attachfile,$mydocfile.$filename);   
 8}   
 9if(!empty($gdocfile)){   
10//echo "cp ".$attachfile." ".$gdocfile.$filename;   
11copy($attachfile,$gdocfile.$filename);   
12}   
13if(!empty($jishufile)){   
14//echo "cp ".$attachfile." ".$gdocfile.$filename;   
15copy($attachfile,$jishufile.$filename);   
16}   
17}   
1<html>
2<head>
3<title> :::::: </title>
4<meta content="text/html; charset=utf-8; 
5```" http-equiv="Content-Type"/&gt;   

echo ${css_.$SAS_SESSION[language]};

 1  
 2&lt;script language="javascript"&gt;   
 3function sendform(){   
 4if(document.dataform.attachfile.value==''){   
 5alert("请添加上传文件!");   
 6document.dataform.attachfile.focus();   
 7return false;   
 8}   
 9  
10var fname='';   
11var str=document.dataform.attachfile.value;   
12fname=str.split("\\\");   
13document.dataform.filename.value=fname[fname.length-1];   
14document.dataform.submit();   
15  
16}   
17&lt;/script&gt;
18&lt;/head&gt;
19&lt;body leftmargin="0" marginheight="0" marginwidth="0" topmargin="0"&gt;
20&lt;center&gt;
21&lt;table border="0" cellpadding="0" cellspacing="0" width="95%"&gt;
22&lt;tr&gt;
23&lt;td class="Td8"&gt;
24&lt;table border="0" cellpadding="0" cellspacing="0" width="0"&gt;
25&lt;tr&gt;
26&lt;td class="Td8" width="19"&gt; &lt;/td&gt;
27&lt;td class="Td8a"&gt;&lt;b&gt;文档管理--文件上传&lt;/b&gt;&lt;/td&gt;
28&lt;td class="Td8" width="48"&gt; &lt;/td&gt;
29&lt;td class="Td8"&gt; &lt;/td&gt;
30&lt;/tr&gt;
31&lt;/table&gt;
32&lt;/td&gt;
33&lt;td class="Td8" width="10"&gt; &lt;/td&gt;
34&lt;/tr&gt;
35&lt;/table&gt;
36&lt;/center&gt;
37&lt;form action="```
38 echo $PHP_SELF;
39```" enctype="multipart/form-data" method="post" name="dataform"&gt;
40&lt;input name="filename" type="hidden"/&gt;
41&lt;center&gt;
42&lt;table border="0" cellpadding="0" cellspacing="1" class="Font12px" width="95%"&gt;
43&lt;tr align="center"&gt;&lt;td&gt;
44&lt;input ".$sas_session[userlogin][uloginid]."="" ";="" "checked";="" ```="" ```"="" class="ip2" if(!empty($mydocfile)="" name="mydocfile" type="checkbox" user="" value="```
45 echo $path-&gt;userfiles." ¦="" ¦(empty($mydocfile)&&empty($gdocfile)))echo=""/>   
46上传到我的文件夹      
47<input ".$sas_session[userlogin][ugroupid]."="" ";="" "checked";="" ```="" ```"="" class="ip2" echo="" group="" if(!empty($gdocfile))="" name="gdocfile" type="checkbox" value="```
48 echo $path-&amp;gt;userfiles."/&gt;   
49上传到部门文件夹   
50&lt;input ";="" "checked";="" ```="" ```"="" class="ip2" echo="" if(!empty($gdocfile))="" jishu="" name="jishufile" type="checkbox" value="```
51 echo $path-&gt;userfiles."/>   
52上传到技术文件夹   
53</td></tr>
54<tr align="center"><td>
55<input name="attachfile" size="30" type="file"/>   
56 <input class="ipSub" name="cmdUp" onclick="javascript:sendform();" type="button" value="提 交"/>
57</td></tr></table>
58</center>
59</form>
60</body>
61</html>

---------------------------------------------------------------

以下摘自PHP手册

上传表单:

1<form action="_URL_" enctype="multipart/form-data" method="post">
2<input name="MAX_FILE_SIZE" type="hidden" value="1000"/>   
3Send this file: <input name="userfile" type="file"/>
4<input type="submit" value="Send File"/>
5</form>

我们推荐您使用 $_FILES。……

以上范例中 $_FILES 数组的内容如下所示。我们假设文件上传字段的名称如上例所示,为 userfile。

$_FILES['userfile']['name']
客户端机器文件的原名称。

$_FILES['userfile']['type']
文件的 MIME 类型,需要浏览器提供该信息的支持,例如“image/gif”。

$_FILES['userfile']['size']
已上传文件的大小,单位为字节。

$_FILES['userfile']['tmp_name']
文件被上传后在服务端储存的临时文件名。

$_FILES['userfile']['error']

可以用$uploadfile_name$uploadfile_type等等来解决,upload代表上传文件,例如问题中的:pic

Published At
Categories with Web编程
Tagged with
comments powered by Disqus