PHP文件上传

10 篇文章 0 订阅
2 篇文章 0 订阅

PHP文件上传

通过文件上传的例子,了解文件上传的过程以及所需要的php中的$_FILES全局数组的使用

PHP 的全局数组 $_FILES

第一个参数是表单的 input name,第二个下标可以是 “name”、“type”、“size”、“tmp_name” 或 “error”。如下所示

$_files全局数组意义
$_FILES[“file”][“name”]上传文件的名称
$_FILES[“file”][“type”]上传文件的类型
$_FILES[“file”][“size”]上传文件的大小、以字节记
$_FILES[“file”][“tmp_name”]存储在服务器的文件的临时副本的名称
$_FILES[“file”][“error”]由文件上传导致的错误代码

表单结构

<!--文件名称  manyFilee.php-->
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>多文件上传</title>
</head>
<body>
<form action="manyFiles_ok.php" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td>内容1</td>
            <td width="369">
                <input type="file" name="picture[]" size="30">
            </td>
        </tr>
        <tr>
            <td>内容2</td>
            <td width="369">
                <input type="file" name="picture[]" size="30">
            </td>
        </tr>
        <tr>
            <td>内容3</td>
            <td width="369">
                <input type="file" name="picture[]" size="30">
            </td>
        </tr>
        <tr>
            <td>内容4</td>
            <td width="369">
                <input type="file" name="picture[]" size="30">
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <button type="submit">提交</button>
            </td>
        </tr>
    </table>

</form>
</body>
</html>

php上传文件

<?php

//判断文件上传的目录
if (!is_dir("./upfile")) {
    mkdir("./upfile");
}

//去除文件中相同的文件名
$array = array_unique($_FILES["picture"]["name"]);

//上传文件名
foreach ($array as $k => $v) {
    $path = "upfile/" . $v;
    if ($v) {
        if (move_uploaded_file($_FILES["picture"]["tmp_name"][$k], $path)) {
            $result = true;
        } else {
            $result = false;
        }
    }
}

//判断文件是否上传成功
if ($result == true) {
    echo "文件上传成功,请稍等......";
    echo "<meta http-equiv=\"refresh\" content=\"3,url=manyFiles.php\">";
} else {
    echo "文件上传失败,请稍等......";
    echo "<meta http-equiv=\"refresh\" content=\"3,url=manyFiles.php\">";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值