doufang6268 2019-01-17 20:47
浏览 42
已采纳

不上传多个文件

I borrowed this code from another StackOverflow question, yet for some reason I cannot get it to work. When testing it is not even passing the files. My error testing shows

total: 0

So it fails right there. Any help would be greatly appreciated

session_start();
$total = count($_FILES['upload']['name']);
echo "total: " . $total;
    for( $i=0 ; $i < $total ; $i++ ) {
    $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
    echo $tempFilePath . "<br>";
    if ($tmpFilePath != ""){
        $newFilePath = "/images/prod/" . $_FILES['upload']['name'][$i];
        echo $newFilePath . "<br>";
        if(move_uploaded_file($tmpFilePath, $newFilePath)) {
            echo $_FILES['upload']['name'];
        }
    }
}

<form action="addpics.php" method="get" name="add" enctype="multipart/form-data>
<input type="file" name="imgs[]" multiple>
<input type="submit" value=" - ADD - ">
</form>

Expect the files to upload to the /images/prod folder and echo the array of file names

  • 写回答

2条回答 默认 最新

  • dszsajhd237437 2019-01-17 21:03
    关注

    The issue is because your method, enctype & field name is wrong. As others have mentioned you are missing the quote on enctype.

    You are trying to get the upload field from your form but the form doesn't have this field, so you need to update file input and rename it to upload[]

    Also you are trying to submit the form via a GET request which wont pass the data across.

    This code should fix the issue (I've changed it so it posts to itself rather than your addpics.php file)

    <?php
    
    if(isset($_FILES['upload'])){
    session_start();
    $total = count($_FILES['upload']['name']);
    
    echo "total: " . $total;
    for( $i=0 ; $i < $total ; $i++ ) {
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
        echo $tempFilePath . "<br>";
        if ($tmpFilePath != ""){
            $newFilePath = "/images/prod/" . $_FILES['upload']['name'][$i];
            echo $newFilePath . "<br>";
            if(move_uploaded_file($tmpFilePath, $newFilePath)) {
                echo $_FILES['upload']['name'];
            }
        }
    }}
    ?>
    <form method="post" name="add" enctype="multipart/form-data">
    <input type="file" name="upload[]" multiple>
    <input type="submit" value=" - ADD - ">
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题