简单的文件上传.
如果有多个文件的话,可能稍微复杂点.
<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);
//入库
...
}
}
如果有多个文件的话,可能稍微复杂点.
<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);
//入库
...
}
}
解决方案 »
- 我要注册或者登陆后,跳转到当前浏览的页面,怎么实现?
- 关于PHP的exec函数
- php输出pdf问题
- PHP精鹰超级群43016622,百G视频书籍陆续加入!
- 目录indexphp如何调用到首页index.php显示?
- PHP模拟向指定URL发送数据,在POST数据的同时,如何发送先前获取的COOIE,SESSION
- require("template/error.php?error=11")的问题,在线等候,解决马上给分。
- 大家一般是按什么方法给网站报价的?
- (新手)PHP怎么没有幂运算,只好自己写了一个函数
- 怎么在linux下配好php?
- php5编译with-oci8重启apache出错
- 如何用 PHP 对文本文件进行筛选式修改?具体内容见文章,谢谢!
$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/
后面是空的
请问哪里错了
escape一下。
楼主这个代码好像有SQL注入漏洞哦。楼主用mysql_real_escape_string一下那四个变量,防止写入数据错误。然后,楼主把那个表单也贴出来,看看。
$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();
}楼主把表单贴出来看看。
产品添加<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>这个了
怪不得会这样,把那个表单改为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>
$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);
}}
是img文件夹而不是文件吧。<?php
if(move_uploaded_file($_FILES['userfile']["tmp_name"],"./img/".$_FILES['userfile']['name']))
{
echo "移动文件成功";
}
else
{
echo "移动失败";
}
?>