【问题标题】:Upload 2 or more images PHP上传 2 张或更多图片 PHP
【发布时间】:2017-11-22 22:53:47
【问题描述】:

我需要上传几张图片,我有2个问题,第一个不允许我从手机上传2个或更多文件(这里很重要,如果是从手机你必须打开相机,如果它在 PC 中,它必须显示文件选择窗口,这工作正常,但使用手机它只留下一个,到目前为止我只在 Android 上使用 Crhome 尝试过),第二个细节是第一个元素没有保存,如果它只是一个文件,因为它也没有,它似乎没有占据位置 [0],当我放置多个图像时,第一个不保存,其他的被正确保存.我已经尝试了一段时间,但我没有看到问题。附上我的文件结构: \相机
└───上传
└───index.php
└───upload.php

index.php:

<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>

还有上传.php:

<?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;
            }
        ?>

【问题讨论】:

    标签: php html post web


    【解决方案1】:

    这对我有用,Hop 这将解决您的第二个问题。

    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;
                            }
                          }
                        }
    

    【讨论】:

    • 您的 reArrayFiles 看起来有问题。 count($file['name']) 不会告诉你上传了多少文件。
    • 同样的问题,第一张图片有错误u.u
    • 完整代码的结果,在这里可以正常工作。 Array ( [0] =&gt; Array ( [name] =&gt; 25.jpg [type] =&gt; image/jpeg [tmp_name] =&gt; /tmp/phpxGVpJt [error] =&gt; 0 [size] =&gt; 94727 ) [1] =&gt; Array ( [name] =&gt; 26.jpg [type] =&gt; image/jpeg [tmp_name] =&gt; /tmp/phpH4oPuq [error] =&gt; 0 [size] =&gt; 73318 ) )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 2021-12-01
    • 2017-06-15
    • 1970-01-01
    相关资源
    最近更新 更多