Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用PHP将多个文件上载到文件夹中_Php - Fatal编程技术网

无法使用PHP将多个文件上载到文件夹中

无法使用PHP将多个文件上载到文件夹中,php,Php,我需要一些帮助。我需要上传多个文件到文件夹使用PHP。在这里我只能上传一个文件。我在下面解释我的代码 <input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" name="bannerimage" id="bannerimage"> <input style="padding:0;" type="file" class="filestyle form-control"

我需要一些帮助。我需要上传多个文件到文件夹使用PHP。在这里我只能上传一个文件。我在下面解释我的代码

 <input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" name="bannerimage" id="bannerimage">
<input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" name="bannerimage1" id="bannerimage1">

这是我的PHP代码

<?php
 $imageName='bannerimage';
 $imagePath="uploads/";
 if ($_FILES['bannerimage']['name'] != ""){
    uploadImage($_FILES,$imageName,$imagePath,function($image){
       $newCustomerobj->image =$image['img'];
    }
 }
  public function uploadImage($files, $imageFieldName, $imageDirPath, $callback) {
        $result = array();
        //  print_r( $_FILES);exit;
        $imageName = generateRandomNumber() . '_' . $_FILES[$imageFieldName]['name'];
        // echo($_FILES[$imageFieldName]['tmp_name']);exit;
        $target_dir = $imageDirPath;
        $target_file = $target_dir . basename($imageName);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
        if (file_exists($target_file)) {
            $result['msg'] = "Sorry, file already exists.";
            $result['num'] = 0;
            $callback($result);
            $uploadOk = 0;
        }
        if ($_FILES[$imageFieldName]["size"] > 500000) {
            //  echo 'fileSize';exit;
            $result['msg'] = "Sorry, file size is large.";
            $result['num'] = 0;
            $callback($result);
            $uploadOk = 0;
        }
        if (($imageFileType != "jpg" && $imageFileType != "JPG") && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
            $result['msg'] = "Sorry, only .jpg,.jpeg,.gif and .png files are allowed.";
            $result['num'] = 0;
            $callback($result);
            $uploadOk = 0;
        }
        if ($uploadOk == 0) {
            $result['msg'] = "Sorry, Your file could not uploaded.";
            $result['num'] = 0;
            $callback($result);
        } else {
            if (move_uploaded_file($_FILES[$imageFieldName]['tmp_name'], $target_file)) {
                $result['msg'] = "Image has uploaded successfully.";
                $result['num'] = 1;
                $result['img'] = $imageName;
                $callback($result);
            } else {
                $result['msg'] = "Sorry, Your Image could not uploaded to the directory.";
                $result['num'] = 0;
                $callback($result);
            }
        }
    }
?>
使用

多重属性是一个布尔属性

当存在时,它指定可以同时选择多个选项

HTML输入

<input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" multiple name="bannerimage[]" id="bannerimage">

或完整示例:-请参考,然后在php端尝试打印$\u文件。您将在html表单中获得答案,将您的姓名更改为
files[]
我的视图中有多个字段。如果是这样的
bannerimage[]
,那么如何识别哪个图像来自哪个文件字段。
[]
意味着您将获得多个图像,您需要在阵列上循环并上载每个图像。是的,但是你在这里使用了
multiple
属性,如果我以后显示它,我还需要知道哪个图像属于哪个字段。如果你想这样做,只需调用
uploadImage()
两次。我可以在php文件中得到像
$\u文件['bannerimage']['name']和$\u文件['bannerimage1']['name']
这样的文件吗。
 // set all images into array
function reArrayFiles(&$file_post)
{

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i = 0; $i < $file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}


if ($_FILES['bannerimage']['name'] != "") {
    $file_ary = reArrayFiles($_FILES['bannerimage']);
    foreach ($file_ary as $file) {
        uploadImage($file, $imageName, $imagePath, function ($image) {
            $newCustomerobj->image = $image['img'];
        }
    }
}
<input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" name="bannerimage" id="bannerimage">
<input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" name="bannerimage1" id="bannerimage1">
$imageName = 'bannerimage';
$imageName2 = 'bannerimage1'; // for bannerimage1
$imagePath = "uploads/";

// uploading bannerimage
if ($_FILES['bannerimage']['name'] != "") {
    uploadImage($_FILES, $imageName, $imagePath, function ($image) {
        $newCustomerobj->image = $image['img'];
    }
 }

 // uploading bannerimage1
if ($_FILES['bannerimage1']['name'] != "") {
    uploadImage($_FILES, $imageName2, $imagePath, function ($image) {
        $newCustomerobj->image = $image['img'];
    }
 }