PHP的一个简单的文件上传功能

文件上传使用的是move_uploaded_file()函数,使用array_push()函数向数组中添加元素,使用array_unique()函数删除数组中的重复元素,使用array_pop()函数获取数组中最后一个元素,并将数组长度减1,使用count()函数获取数组的元素个数。


主要代码:

页面:

<table width="725" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td colspan="2"><img src="images/02-03.jpg" width="765" height="230"></td>
    </tr>
    <tr>
        <td width="662" align="center" valign="top"><table width="578" border="0" cellspacing="0" cellpadding="0">

                <tr>
                    <td height="20" colspan="2" align="center" bgcolor="#FFFFFF"><img src="images/02-03 (1).jpg" width="561" height="42"></td>
                </tr>
                <tr>
                    <td height="20" colspan="2" align="center" bgcolor="#FFFFFF" class="STYLE1">文件路径(5个文件以内任意上传)</td>
                </tr>
                <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 height="30" align="right" class="STYLE1">内容2</td>
                        <td><input name="picture[]" type="file" id="picture[]" size="30"></td>
                    </tr>
                    <tr>
                        <td height="30" align="right" class="STYLE1">内容3</td>
                        <td><input name="picture[]" type="file" id="picture[]" size="30"></td>
                    </tr>
                    <tr>
                        <td height="30" align="right" class="STYLE1">内容4</td>
                        <td><input name="picture[]" type="file" id="picture[]" size="30"></td>
                    </tr>
                    <tr>
                        <td height="30" align="right" class="STYLE1">内容5</td>
                        <td><input name="picture[]" type="file" id="picture[]" size="30"></td>
                    </tr>
                    <tr>
                        <td height="50">&nbsp;</td>
                        <td><input type="image" name="imageField" src="images/02-03 (3).jpg"></td>
                    </tr>
                </form>
            </table></td>
        <td width="99"><img src="images/02-03 (2).jpg" width="204" height="362"></td>
    </tr>
    <tr>
        <td colspan="2"><img src="images/02-03 (4).jpg" width="765" height="80"></td>
    </tr>
</table>

PHP代码:

<?php
if(!is_dir("./upfile")){                     //判断服务器中是否存在指定文件夹
    mkdir("./upfile");                      //如果不存在,则创建文件夹
}
array_push($_FILES["picture"]["name"],"");       //向表单提交的数组中增加一个空元素
$array=array_unique($_FILES["picture"]["name"]);    //删除数组中重复的值
array_pop($array);                                //删除数组中最后一个单元
for($i=0;$i<count($array);$i++){               //根据元素个数执行for循环
    $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\">";
}

?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值