一 test2.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>test2</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> </head> <body> <form action="test3.php" method="POST"> <input value='' type="hidden" id='getpath' name='x1' /> <input value='sdf' name='x2' /> <div class="parent"> <input type="file" class="file"> </div> <input type="submit" value="Submit" /> </form> </body> </html> <script> $(".parent").on("change",".file",function(){ var getpath=$(this).val(); alert(getpath); $('#filepath').val(getpath); }); </script>
用parent把file input包起来是为了防止onchange事件一次调用之后失效.
二 test3.php
<?php error_log('WBW'.'|'.date('Y-m-d H:i:s', time()).'|'.print_r($_REQUEST,1)."\r\n",3,'/tmp/debug.log');
本人用的ubuntu系统,以上为将表单各个参数打印到临时日志文件中.
三 结果展示
①点击浏览按钮,并选择文件
②点击submit按钮,并选择文件确定
wbw@wbw-All-Series:/tmp$ tail -f debug.log
[x1] => C:\fakepath\140C.xlsx
[x2] => sdf
)
WBW|2019-12-24 18:36:45|Array
(
[x1] => C:\fakepath\ll.txt
[x2] => sdf
)
获取文件绝对路径成功!