首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >上传2张或更多图片PHP

上传2张或更多图片PHP
EN

Stack Overflow用户
提问于 2017-11-23 06:53:48
回答 1查看 57关注 0票数 0

我需要上传几张图片,我有两个问题,第一个不允许我上传2个或更多的文件,当它是从手机(这里是重要的东西,如果它来自手机,你必须打开相机,如果它是在PC上,它必须显示文件选择窗口,这很好,但对于手机,它只留下一个,到目前为止,我只尝试在Android上使用Crhome)和第二个细节是,第一个元素没有保存,如果它只是一个文件,因为它也不是,它似乎不采取位置,当我放置多个图像时,第一个图像不会保存,其他图像会正确保存。我已经尝试了一段时间,但我看不出有什么问题。附件我的文件结构:\camera

└───上传

└───index.php

└───upload.php

index.php:

代码语言:javascript
运行
复制
<html>
<head>
    <meta charset="UTF-8">
    <title>upload</title>
</head>
<body>
    <form action="upload.php" method="post" multipart="" enctype="multipart/form-data">
        <input type="file" name="img[]" accept="image/*" id="capture" capture="camera" multiple >
        <input type="submit">
    </form>
</body>
</html>

和upload.php:

代码语言:javascript
运行
复制
<?php
            echo '<pre>';
            $img = $_FILES['img'];

            if(!empty($img))
            {
                $img_desc = reArrayFiles($img);
                print_r($img_desc);

                foreach($img_desc as $val)
                {
                    $newname = date('YmdHis',time()).mt_rand().'.jpg';
                    move_uploaded_file($val['tmp_name'],'./uploads/'.$newname);
                }
            }

            function reArrayFiles($file)
            {
                $file_ary = array();
                $file_count = count($file['name']);
                $file_key = array_keys($file);

                for($i=0;$i<$file_count;$i++)
                {
                    foreach($file_key as $val)
                    {
                        $file_ary[$i][$val] = $file[$val][$i];
                    }
                }
                return $file_ary;
            }
        ?>
EN

Stack Overflow用户

发布于 2017-11-23 07:10:47

这对我很有效,Hop这将解决你的第二个问题。

代码语言:javascript
运行
复制
if (isset($_FILES['Gallery']) && is_array($_FILES['Gallery'])) {
                      $errors= array();
                      foreach($_FILES['Gallery']['tmp_name'] as $key => $tmp_name ) {
                        $file_name = $key.$_FILES['Gallery']['name'][$key];
                        $file_size =$_FILES['Gallery']['size'][$key];
                        $file_tmp =$_FILES['Gallery']['tmp_name'][$key];
                        $file_type=$_FILES['Gallery']['type'][$key];
                        if($file_size > 2097152){
                          $errors[]='File size must be less than 2 MB';
                        }
                        if (empty($errors)==true) {
                          if (is_dir('uploads')==false) {
                            mkdir('uploads', 0700);     // Create directory if it does not exist
                          }

                          if (file_exists("uploads/".$file_name)==false) {
                            move_uploaded_file($file_tmp,"uploads/".$file_name);
                            chmod("uploads/".$filename, 0777);
                            $Gallery_Link = "uploads/".$file_name;
                          } else {                                  // rename the file if another one exist
                            $Gallery_Link = "uploads/".time()."_".$file_name;
                            rename($file_tmp,$Gallery_Link) ;
                          }

                        } else {
                          echo $errors;
                        }
                      }
                    }
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47445093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档