---
title: "input file  获得文件根目录"
author: "smallearth"
date: "2013-04-25T17:30:00Z"
tags: []
slug: "input-file"
draft: false
lastmod: "2013-11-30T20:10:13Z"
id: 1983275
url: "https://smallearth.iteye.com/blog/1983275"
---

```

```

```
其实问题很简单
```

```
index.html
```

```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>烟台大学体测</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
   function show_root(){
     //var root = myform.file.value; 
     var root = document.getElementById("file").value;
	 //alert(root);
	 //document.getElementById("root").innerHTML=root;
	 myform.root.value=root;
   }
</script>
</head>
<body>
  <form name="myform" method="post" action="upload.php"> 
     <input id="file" type="file" name="file"/>
     <input id="root" type="hidden" name="root"/>
     <input type="submit" name="submit" onclick="show_root()"/> 
  </form> 
</body>
</html>
```

```
upload.php
```

&lt;?php echo $\_POST\["root"];

下面这段js代码就是获得file根目录，然后赋值给hidden类型input，然后就可以交给php or jsp 处理了

```
<script type="text/javascript">
   function show_root(){
     //var root = myform.file.value; 
     var root = document.getElementById("file").value;
	 //alert(root);
	 //document.getElementById("root").innerHTML=root;
	 myform.root.value=root;
   }
</script>
```

![](http://img.my.csdn.net/uploads/201304/25/1366882543_1379.png)

![](http://img.my.csdn.net/uploads/201304/25/1366882548_7142.png)