PHP Multiple File Array

前端 未结 6 1191
陌清茗
陌清茗 2020-12-03 09:29

I have the following code which works and uploads but it will not cycle through the array to upload every file, just the first file.

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 09:54

    For anyone trying to do it with a single file php function (i`m using classes, but you can change to a function):

    html:

                            
                            
                            
                            
                            
    

    php:

    if (isset($_FILES['foto'])) {
    
          $arquivo = array();
        foreach ($_FILES['foto']["name"] as $file=>$key) {
    
                        // the empty input files create an array index too, so we need to
                        // check if the name exits. It means the file exists.
            if (!empty($_FILES['foto']["name"][$file])) {
              $arquivo ["name"] = $_FILES['foto']["name"][$file];
              $arquivo ["type"] = $_FILES['foto']["type"][$file];
              $arquivo ["tmp_name"] = $_FILES['foto']["tmp_name"][$file];
              $arquivo ["error"] = $_FILES['foto']["error"][$file];
              $arquivo ["size"] = $_FILES['foto']["size"][$file];
    
    $foto = new foto(); // create an obj foto
        // $arquivo means file, it`s our file format as a single $_file['file']
    if ($foto -> upload($arquivo)) { // if its uploaded than save
        $foto -> save();
    }
    
    
        }
    
        } 
    
    }
    

    on my foto class:

    public function upload($foto) {
    
        $upload_dir = "D:/xampp/htdocs/prova/fotos/";
        $file_dir = $upload_dir . $foto["name"];
    
        $move = move_uploaded_file($foto["tmp_name"], $file_dir);
        $this -> arquivo = $foto["name"]; // use this to save to db later
    
        // this serves to return true if the file is uploaded
        $retorno = ($move) ? 1 : 0; 
        return $retorno;
    
    }
    

提交回复
热议问题