当前位置:编程学习 > php >>

求一php文件上传代码,需要获取到文件的路径。

 

如图,一个表单,其中有一项是实现缩略图上传。点击上传后得到文件路径。表单提交到另一个php页面以后得到文件路径,和其他项的值。  难点在于上传怎么处理,外部已经有一个form了。
给出相关的一些代码如下,可以直接在上面改。
------------表单页面add.php---------------
<html>
<head></head>
<body>
 <form action="roomaction.php?action=insertRoom" method="post" enctype="multipart/form-data">
  <table align="center" width="600" cellpadding="0" border="1" cellspacing="0">
   <tr height="35" >
    <td align="right">房间类型:</td>
    <td><input type="text" name="rType"></td>
   </tr>
   <tr height="35" >
    <td align="right">房间价格:</td>
    <td><input type="text" name="rPrice"></td>
   </tr>
   <tr height="35" >
    <td align="right">缩略图:</td>
    <td><input type="file" name="rPicurl" id="rPicurl" style="height:21px;"/><input type="button" value="上传">
    </td>
   </tr>
   <tr height="35">
    <td colspan="2" align="center"><input type="submit" value="添加类型"></td>
   </tr>
  </table>
 </form>
</body>
<html>
------------表单接受页面action.php--------------------
$action = $_GET["action"];
if($action == "insertRoom"){
 $rType = $_POST["rType"];
 $rPrice = $_POST["rPrice"];
 $rPicurl = $_POST["rPicurl"];
        echo $rpicurl;
        echo  "<br>";
        echo $rType;
}
---------------文件上传处理页面upload.php----------------
<?php
//---------多图片上传----------
function getExtName($file){         //获得文件括展名
 $ext = pathinfo($file);
 print_r($ext);
 return $ext["extension"];
}
function getNewName(){
 $newName = date("YmdHis").md5(mt_rand(10000,45333));
 return $newName;
}
$pic = $_FILES["userupload"];             //接收文件上传
foreach($pic as $key=>$val){
 echo "{$key}=>";
 print_r($pic[$key]);           //打印数组
 echo "<br>";
}
echo  "**********<br>";
foreach($pic["name"] as $key=>$val){
 echo $key."<br>";      //如果库key==0,则$pic[""][$key] 都是指向第一图片的
if($pic["error"][$key]==0){       //此时指向第一个文件
$newName = "upload/".getNewName().".".getExtName($pic["name"][$key]);
 move_uploaded_file($pic["tmp_name"][$key],$newName); //把上传来的图片从临时位置移动到指定位置。

}

补充:文件上传处理页面upload.php  
$pic = $_FILES["userupload"]; //接收文件上传
这句要改为$pic = $_FILES["rPrice"];
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,