简单的文件上传.
如果有多个文件的话,可能稍微复杂点.
<form name="input" method="post" action="" enctype="multipart/form-data" >
pic1:<input type='file' name='pic[]' value='' size='35'>
pic2:<input type='file' name='pic[]' value='' size='35'>
pic3:<input type='file' name='pic[]' value='' size='35'>
<input type='submit' name='submit' value='submit'/>
</form>//php
$arrPic = $_FILES['pic'];
if(is_array($arrPic))
{
//获得系统路径,如果没有则建立
$tmpDir = _ATTACH_DIR . date('ymd') . "/" . $id . "/";
  if(!is_dir($tmpDir))
{
mkdir($tmpDir, 0777, true);
}
$arrFileName = $_FILES['pic']['name'];
$arrTmpName = $_FILES['pic']['tmp_name'];
$arrError = $_FILES['pic']['error'];
foreach($arrFileName as $key=>$value)
{
if($arrError[$key] != 0)
{
echo $arrError[$key];
continue;
}
$strExtend = "." .  pathinfo($value, PATHINFO_EXTENSION);
$strSrcFileName = $arrTmpName[$key];
$strClientFileName = $arrFileName[$key];
    //getRandomeName为一个生成随机文件名的函数
$strFileName = $this->getRandomName() . $strExtend;
$strDestName = $tmpDir . $strFileName;
move_uploaded_file($strSrcFileName, $strDestName);
    //入库
    ...
}
}

解决方案 »

  1.   

    $Pname    = $_REQUEST['Pname'];
    $Pno      = $_REQUEST['Pno'];
    $Price    = $_REQUEST['Price'];
    $Purl = "./img/".$_FILES['userfile']['name'];include ("homeconfig.php");
    mysql_query("SET NAMES UTF8"); //解决中文乱码问题$query="insert into chanpin (Pname,Pno,Price,Purl) values ('$Pname','$Pno','$Price','$Purl')";
    $result=mysql_query($query);if(!mysql_error()){
    echo "<script language=javascript>alert('OK');</script>"; 
    }else{
    echo mysql_error();
    }我这么写了  但是数据库里 没有获取到  文件的名字  只进去了产品信息那些
     路径那个地方是 ./img/
      后面是空的
     请问哪里错了  
      

  2.   

    是不是$_FILES['userfile']['name']里面的数值带有但因号之类的。
    escape一下。
      

  3.   

    没有引号啊, 就是正常的 一个读取路径的啊 <input name="userfile" type="file" />
      

  4.   


    楼主这个代码好像有SQL注入漏洞哦。楼主用mysql_real_escape_string一下那四个变量,防止写入数据错误。然后,楼主把那个表单也贴出来,看看。
      

  5.   

    过滤并转义特殊字符。$Pname    = $_REQUEST['Pname'];
    $Pno      = $_REQUEST['Pno'];
    $Price    = $_REQUEST['Price'];
    $Purl = "./img/".$_FILES['userfile']['name'];include ("homeconfig.php");
    mysql_query("SET NAMES UTF8"); //解决中文乱码问题$Pname=mysql_real_escape_string($Pname);
    $Pno=mysql_real_escape_string($Pno);
    $Price=mysql_real_escape_string($Price);
    $Purl=mysql_real_escape_string($Purl);$query="insert into chanpin (Pname,Pno,Price,Purl) values ('$Pname','$Pno','$Price','$Purl')";
    $result=mysql_query($query);if(!mysql_error()){
    echo " <script language=javascript>alert('OK'); </script>";
    }else{
    echo mysql_error();
    }楼主把表单贴出来看看。
      

  6.   

    <form name="form1" method="get" action="insert.php">
    产品添加<br>产品名称<input name="Pname" type="text" size="13" maxlength="12">
    <br>产品编号<input name="Pno" type="text" size="13" maxlength="12">
    <br>
    产品价格<input name="Price" type="text" size="13" maxlength="12">
    <br>
    产品图片<input name="userfile" type="file" />
    <br><input type="submit" name="Submit" value="添加">
    </form>这个了
      

  7.   


    怪不得会这样,把那个表单改为POST方式传送的。

    <form name="form1" method="post" action="insert.php">
    产品添加 <br>产品名称 <input name="Pname" type="text" size="13" maxlength="12">
    <br>产品编号 <input name="Pno" type="text" size="13" maxlength="12">
    <br>
    产品价格 <input name="Price" type="text" size="13" maxlength="12">
    <br>
    产品图片 <input name="userfile" type="file" />
    <br><input type="submit" name="Submit" value="添加">
    </form> 
      

  8.   

    OK了, 文件名入库了, 谢谢9楼 不过  下面我要把  这个图片  上传到  img文件里,应该怎么写?
      

  9.   

    我曾经用过的,你参考一下吧!
    $max_file_size=5000000;   //上传文件大小限制, 单位BYTE
    $destination_folder="upload/"; //上传文件路径
    $water=0;   //是否附加水印(1为加水印,其他为不加水印);
    $watertype=1;   //水印类型(1为文字,2为图片)
    $waterposition=1;   //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
    $waterstring="coocor"; //水印字符串
    $waterimg="xplore.gif";  //水印图片
    $imgpreview=1;   //是否生成预览图(1为生成,其他为不生成);
    $imgpreviewsize=1/2;  //缩略图比例/*图片上传*/
    if (is_uploaded_file($_FILES["upfile"][tmp_name])){//是否存在文件 if($max_file_size < $file["size"]){//检查文件大小
    $url="bof_menu_add.php?state_error='".urlencode('picture')."\'";
    echo "<SCRIPT>window.location   =   \"".$url."\";   </SCRIPT>"; 
    } if(!file_exists($destination_folder))
    mkdir($destination_folder); $file = $_FILES["upfile"];
    $filename=$file["tmp_name"];
    $image_size = getimagesize($filename);
    $pinfo=pathinfo($file["name"]);
    $ftype=$pinfo[extension];
    $destination = $destination_folder.time().".".$ftype; if (file_exists($destination) && $overwrite != true) 
    {
     $url="bof_material_add.php?state='picture is allready exist!'";
     echo "<SCRIPT>window.location   =   \"".$url."\";   </SCRIPT>"; 
    } if(!move_uploaded_file ($filename, $destination))
    {
     $url="bof_material_add.php?state='there hava something wrong happen!'";
     echo "<SCRIPT>window.location   =   \"".$url."\";   </SCRIPT>";
    } $pinfo=pathinfo($destination);
    $fname=$pinfo[basename]; if($water==1)
    {
    $iinfo=getimagesize($destination,$iinfo);
    $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
    $white=imagecolorallocate($nimage,255,255,255);
    $black=imagecolorallocate($nimage,0,0,0);
    $red=imagecolorallocate($nimage,255,0,0);
    imagefill($nimage,0,0,$white); 
    switch ($iinfo[2])
    {
    case 1:
    $simage =imagecreatefromgif($destination);
    break;
    case 2:
    $simage =imagecreatefromjpeg($destination);
    break;
    case 3:
    $simage =imagecreatefrompng($destination);
    break;
    case 6:
    $simage =imagecreatefromwbmp($destination);
    break;
    default:
    die("<font color='red'>不能上传此类型文件!</a>");
    exit;
    } imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
    imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); switch($watertype)
    {
    case 1:  //加水印字符串
    imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
    break;
    case 2:  //加水印图片
    $simage1 =imagecreatefromgif("xplore.gif");
    imagecopy($nimage,$simage1,0,0,0,0,85,15);
    imagedestroy($simage1);
    break;
    }  switch ($iinfo[2])
    {
    case 1:
    //imagegif($nimage, $destination); 
    imagejpeg($nimage, $destination);
    break;
    case 2:
    imagejpeg($nimage, $destination);
    break;
    case 3:
    imagepng($nimage, $destination);
    break;
    case 6:
    imagewbmp($nimage, $destination);
    //imagejpeg($nimage, $destination);
    break;
    } //覆盖原上传文件
    imagedestroy($nimage);
    imagedestroy($simage);
    }}
      

  10.   


    是img文件夹而不是文件吧。<?php
    if(move_uploaded_file($_FILES['userfile']["tmp_name"],"./img/".$_FILES['userfile']['name']))
    {
    echo "移动文件成功";
    }
    else
    {
    echo "移动失败";
    }
    ?>