只上传一张图片的功能已经实现,上传两张图片就实现不了,代码段比较简单,麻烦大家指导一下:上传图片的表单:
<?php
if ( isset($_GET['action']) && $_GET['action'] == "save" )
{
include_once('conn.php'); //连接数据库
include_once('uploadclass.php');//这个文件在下文会给出$pic=$uploadfile; //第一张图片,已经可以正常上传
$pic2=$uploadfile2; //第二张,上传不了$sql="insert into product (pic,pic2) values ('$pic','$pic2')";
$result=mysql_query($sql,$conn);
if (!$result)
{
echo mysql_error();
echo $sql;
exit;
}
else
{
echo"<Script>window.alert('congratulations,the product data inserted successful');location.href='index.php'</Script>";
} mysql_close();
}?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Administrator for upload the product resource</title>
</head>
<body><form method="post" action="?action=save" enctype="multipart/form-data" >Images_1:
<input name="file" type="file" value="Browse" />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<br />
Images_2:
<input name="file2" type="file" value="Browse" />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<p />
<input type="submit" value="upload this resource" name="upload" /></form>
</body>
</html>
上传程序:uploadclass.php(此程序只能上传图片1,如果要能上传图片2,需要怎么加代码呢?)
<?php
$uploaddir = "../images/upfiles/";
$type=array("jpg","gif","bmp","jpeg","png");
$patch="aloesky/";function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}function random($length)
{
$hash = 'CR-';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}$a=strtolower(fileext($_FILES['file']['name']));
if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type))
{
$text=implode(",",$type);
echo "You can only upload the following types of files:",$text,"<br /> <a href='upload_pro.php'>return and try again?click me</a>";
exit;
}
else{
$filename=explode(".",$_FILES['file']['name']);
do
{
$filename[0]=random(10);
$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}while(file_exists($uploadfile));if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile))
{
if(is_uploaded_file($_FILES['file']['tmp_name']))
{echo "the Images Upload failed!";
}
else
{
echo "<center>Your file has been uploaded: </center><br><center><img src='$uploadfile' /></center>";
echo "<br><center><a href='upload_pro.php'>Continue to upload</a></center>";
}
}
}
?>
<?php
if ( isset($_GET['action']) && $_GET['action'] == "save" )
{
include_once('conn.php'); //连接数据库
include_once('uploadclass.php');//这个文件在下文会给出$pic=$uploadfile; //第一张图片,已经可以正常上传
$pic2=$uploadfile2; //第二张,上传不了$sql="insert into product (pic,pic2) values ('$pic','$pic2')";
$result=mysql_query($sql,$conn);
if (!$result)
{
echo mysql_error();
echo $sql;
exit;
}
else
{
echo"<Script>window.alert('congratulations,the product data inserted successful');location.href='index.php'</Script>";
} mysql_close();
}?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Administrator for upload the product resource</title>
</head>
<body><form method="post" action="?action=save" enctype="multipart/form-data" >Images_1:
<input name="file" type="file" value="Browse" />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<br />
Images_2:
<input name="file2" type="file" value="Browse" />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<p />
<input type="submit" value="upload this resource" name="upload" /></form>
</body>
</html>
上传程序:uploadclass.php(此程序只能上传图片1,如果要能上传图片2,需要怎么加代码呢?)
<?php
$uploaddir = "../images/upfiles/";
$type=array("jpg","gif","bmp","jpeg","png");
$patch="aloesky/";function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}function random($length)
{
$hash = 'CR-';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}$a=strtolower(fileext($_FILES['file']['name']));
if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type))
{
$text=implode(",",$type);
echo "You can only upload the following types of files:",$text,"<br /> <a href='upload_pro.php'>return and try again?click me</a>";
exit;
}
else{
$filename=explode(".",$_FILES['file']['name']);
do
{
$filename[0]=random(10);
$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}while(file_exists($uploadfile));if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile))
{
if(is_uploaded_file($_FILES['file']['tmp_name']))
{echo "the Images Upload failed!";
}
else
{
echo "<center>Your file has been uploaded: </center><br><center><img src='$uploadfile' /></center>";
echo "<br><center><a href='upload_pro.php'>Continue to upload</a></center>";
}
}
}
?>
解决方案 »
- 纠结啊,php写了一个方法,根据传值内容相对应关键字加链接。
- 求助 写一个PHP函数,它查找并且返回某个数组的最大值。并附上测试结果
- 购买搜索引擎一般多少钱
- 谁有php406.exe,共享一下.给100分
- 谁能给一个php里显示日期时间的脚本函数
- ***十万火急!!php4.1.0+apache2.0如何备置???***
- 请问可以将网页页面保存为word文档输出吗?
- 我要在一個按鈕點擊后,開啟另外一個頁面,並且這個頁面自動處於打印預覽狀態,怎麼寫?
- 不能运行phpinfo怎么看 当前项目用的是哪个php.ini
- 调用支付宝 批量付款到支付宝账户 的接口,出现证书错误,求助!
- php +swfupload上传图片 500错误怎么解决
- 数组 array_sum()怎么求不了总和?
这样当成数组就好处理了。
php端: print_r($_FILES['file']); //得到一个二维数组,只需处理数组即可.
在这个表单程序里,我还没看懂,PHP是怎样把那文件的路径写进mysql的。
$filename = time() . mt_rand(1000, 9999) . '.' . trim(strtolower(substr(strrchr($filename, '.'), 1)));//对文件进行重命名,以免发生重复文件
move_uploaded_file($_FILES['upload']['name'], $filename);//把文件移动到指定的路径楼主的那些代码还是等高手来插入吧,我也正在PHP入门中
我知道这句:$pic=$uploadfile; //第一张图片,已经可以正常上传
是用来向数据库记录图片路径和文件名的。你说的建立数组我弄不懂。刚接触PHP才一个星期,基本属于刚入门级。能否麻烦你针对我的源码,列一下如果用数组实现多图片上传到指定文件夹下并在数据库记录图片路径,我上面那段抄来的程序应该怎么修改谢谢
你用while或是ajax分多次去调用上传页。对于数据库处理可以一次性的,不过统一分多次吧
既然你一张图片上传能成功,那就绕个弯子:先上传一张,然后再给另外一个界面,用一个主键值关联,对数据库进行update set。这比研究代码块快多了