Multiple file upload in php

前端 未结 14 1632
轮回少年
轮回少年 2020-11-21 11:32

I want to upload multiple files and store them in a folder and get the path and store it in the database... Any good example you looked for doing multiple file upload...

14条回答
  •  执笔经年
    2020-11-21 12:02

    This is what worked for me. I had to upload files, store filenames and I had additional inof from input fields to store as well and one record per multiple file names. I used serialize() then added that to the main sql query.

      class addReminder extends dbconn {
        public function addNewReminder(){
    
               $this->exdate = $_POST['exdate'];
               $this->name = $_POST['name'];
               $this->category = $_POST['category'];
               $this->location = $_POST['location'];
               $this->notes = $_POST['notes'];
    
    
    
               try {
    
                         if(isset($_POST['submit'])){
                           $total = count($_FILES['fileUpload']['tmp_name']);
                           for($i=0;$i<$total;$i++){
                             $fileName = $_FILES['fileUpload']['name'][$i];
                             $ext = pathinfo($fileName, PATHINFO_EXTENSION);
                             $newFileName = md5(uniqid());
                             $fileDest = 'filesUploaded/'.$newFileName.'.'.$ext;
                             $justFileName = $newFileName.'.'.$ext;
                             if($ext === 'pdf' || 'jpeg' || 'JPG'){
                                 move_uploaded_file($_FILES['fileUpload']['tmp_name'][$i], $fileDest);
                                 $this->fileName = array($justFileName);
                                 $this->encodedFileNames = serialize($this->fileName);
                                 var_dump($this->encodedFileNames);
                             }else{
                               echo $fileName . ' Could not be uploaded. Pdfs and jpegs only please';
                             }
                           }
    
                        $sql = "INSERT INTO reminders (exdate, name, category, location, fileUpload, notes) VALUES (:exdate,:name,:category,:location,:fileName,:notes)";
                        $stmt = $this->connect()->prepare($sql);
                        $stmt->bindParam(':exdate', $this->exdate);
                        $stmt->bindParam(':name', $this->name);
                        $stmt->bindParam(':category', $this->category);
                        $stmt->bindParam(':location', $this->location);
                        $stmt->bindParam(':fileName', $this->encodedFileNames);
                        $stmt->bindParam(':notes', $this->notes);
                        $stmt->execute();
                      }
    
               }catch(PDOException $e){
                 echo $e->getMessage();
               }
          }
        }
    

提交回复
热议问题