分享

一个简单的多文件上传例子

 YSY私馆 2013-06-08
第一个文件,index.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css.css" rel="stylesheet" type="text/css" />




<table width="725" border="1" cellpadding="1" cellspacing="1" align="center" cellpadding="0" cellspacing="0">
<form action="index_ok.php" method="post" enctype="multipart/form-data" name="form1">
<tr>
    <td width="88" height="30" align="right" class="STYLE1">内容1:</td>
    <td width="369"><input name="picture[]" type="file" id="picture" size="30" /></td>
    </tr>
<tr>
    <td width="88" height="30" align="right" class="STYLE1">内容2:</td>
    <td width="369"><input name="picture[]" type="file" id="picture" size="30" /></td>
    </tr>
<tr>
    <td width="88" height="30" align="right" class="STYLE1">内容3:</td>
    <td width="369"><input name="picture[]" type="file" id="picture" size="30" /></td>
    </tr>
<tr>
    <td width="88" height="30" align="right" class="STYLE1">内容4:</td>
    <td width="369"><input name="picture[]" type="file" id="picture" size="30" /></td>
    </tr>
<tr>
    <td width="88" height="30" align="right" class="STYLE1">内容5:</td>
    <td width="369"><input name="picture[]" type="file" id="picture" size="30" /></td>
    </tr>
<tr>
    <td colspan="2">            <input type="image" name="imageField" src="images/02-03 (3).jpg" /></td>
    </tr>
</form>
</table>
建立一个表单,设计一下界面
第二个文件,css.css

@CHARSET "UTF-8";

body {
background-color: #F0F0F0;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.STYLE1 {
font-size: 13px;
font-weight: bold;
}
.STYLE2 {font-size: 12px;
color: #FF0000;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}

很简单的一个CSS文件,只是做一些对页面的设置而已

第三个文件,index_ok.php
<?php
if (!is_dir('./upfile')){
mkdir('./upfile');
}

array_push($_FILES['picture']['name'], '');                //向表单提交的数组中增加一个空元素,必须保证就算上传一个也是一个数组,所以给它增加一个空值

$array = array_unique($_FILES['picture']['name']);         //删除数组中重复的值

array_pop($array);                                         //删除数组中的最后一个单元,这里就是把我们给它增加的空值给删除掉

if ($_FILES['picture']['type'] == "image/jpeg"){                                  //判断一下图片的格式是不是jpg,如果不是不能上传
for ($i=0;$i<count($array);$i++){
echo $path="upfile/".$_FILES['picture']['name'][$i];        //定义上传文件的存储位置
if (move_uploaded_file($_FILES['picture']['tmp_name'][$i], $path)){                     //提取出临时文件,并保持在新的存储位置
$result=TRUE;
}else {
$result=FALSE;
}
if ($result == TRUE){                                                                                                //判断图片是否上传成功
echo "文件上传成功,请稍等……";
echo "<meta http-equiv=\"refresh\" content=\"3;url=index.php>";
}else {
echo "文件上传失败,请稍等……";
echo "<meta http-equiv=\"refresh\" content=\"3;url=index.php>";
}
}
}else {
echo "这不是jpg格式的图片,不能上传";
}
?>
主要是用来操作文件的上传


这个例子有三个文件,还是比较简单易懂 ,以下的图片是表格里面的那个
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约