2017-01-01 9 views
0

class.upload.phpを使用して画像をアップロードしている間に、キャプションを挿入する際に現在問題が発生しています。class.upload.phpを使用して複数のファイルとキャプションをアップロード

画像の名前変更はうまくいきましたが、画像ごとに個別のテキストボックスを設定しても、すべての画像に同じキャプションが付きます。

コード:

<form action="" enctype="multipart/form-data" id="form" method="post" name="form" role="form"> 
    <table> 
    <tr> 
     <td><input class="form-control" id="customFieldValue2" name="photo_title[]" type="text"></td> 
     <td><input class="form-control" id="customFieldName2" name="image_field[]" type="file"></td> 
    </tr> 
    <tr> 
     <td><input class="form-control" id="customFieldValue2" name="photo_title[]" type="text"></td> 
     <td><input class="form-control" id="customFieldName2" name="image_field[]" type="file"></td> 
    </tr> 
    </table> 
</form> 

プロセッサ:私は、コードを編集した

$files = array(); 
       foreach ($_FILES['image_field'] as $k => $l) {      
       foreach ($l as $i => $v) { 
       if (!array_key_exists($i, $files)) 
        $files[$i] = array(); 
        $files[$i][$k] = $v; 
       } 
       } 
        $hitung=0; 
        foreach ($files as $file) 
        { 
         $handle = new Upload($file); 
         if ($handle->uploaded) 
         { 
          $hitung++; 
          $newname = $t_timestamp."_".$hitung; 
          $handle->file_new_name_body = $newname; 
          $handle->Process("../uploaded/");  
          if ($handle->processed) 
          { 
           $filename=$newname.".jpg"; 
           $query ="INSERT INTO tbl_report_photos SET 
           photo_title='".$filename."', 
           photo_link='".$filename."', 
           photo_unique='".$aid."'"; 

           if ($sql = mysqli_query($con,$query)) 
           { 
            echo "<script>alert('Report Updated');window.location.href='index.php';</script>"; 
           }  
          } 
          else 
          { 
           echo 'Error: ' . $handle->error;  
          } 
         } 
         else 
         {  
          echo 'Error: ' . $handle->error; 
         } 
         unset($handle); 
        } 

。それは1つ少ないループを持っています

答えて

0

あなたが同じタイトルを持っている理由は、変数を配列していない独自のループですが、むしろそれ自身を上書きします。それは、アレイは、対応するキーを記述してください:あなたは非常に多くのループが起こっている

$taitel[$i] = $_POST['photo_title'][$i]; 

、それはあなたのスクリプトでそれを置くために最良のスポットを示唆するのは難しいです。



EDIT:これを設定する問題が発生しているので、私がデモンストレーションを行います。私が試してみたいことがたくさんあります。 1つは、ファイルオブザーバを作成し、2つは、アップロードクラスの拡張クラスを作成します。その一つが少し厄介なようだ:

UploadObserver.php

/* 
** @description This will take over for a couple of pieces of your script 
*/ 
class UploadObserver 
    { 
     private $Upload; 
     /* 
     ** @description Pass your database. I like PDO and know it better so I am using that connection 
     **     You would have to change this to accept your MySQLi connection 
     */ 
     public function __construct(\PDO $con) 
      { 
       $this->con = $con; 
      } 
     /* 
     ** @description Create a new instance of a version of the Upload class 
     */ 
     public function upload($file) 
      { 
       # Create a new instance of our extended class 
       $this->Upload = new \PowerUpload($file); 
       # Return it for use 
       return $this->Upload; 
      } 
     /* 
     ** @description This is the class that will process the files array 
     */ 
     public function getFiles($key = 'image_field') 
      { 
       $files = array(); 
       foreach ($_FILES[$key] as $k => $l) {      
        foreach ($l as $i => $v) { 
         if (!array_key_exists($i, $files)) 
          $files[$i]  = array(); 

         $files[$i][$k] = $v; 
        } 
       } 
       # Send back formatted array 
       return $files; 
      } 
     /* 
     ** @description Checks to see if the files array has files 
     */ 
     public function filesSet($key = 'image_field') 
      { 
       return (!empty($_FILES[$key])); 
      } 
     /* 
     ** @description This will write the row to your database 
     */ 
     public function saveToDb($settings) 
      { 
       # Make sure to prepare and bind your values just in case 
       $sql ="INSERT INTO tbl_report_photos SET 
       photo_title = ?, 
       photo_link = ?, 
       photo_unique = ?"; 

       $query = $this->con->prepare($sql); 
       $query->execute($settings); 
      } 
     /* 
     ** @description You could store this in a View class, but for sake of ease I included it here 
     */ 
     public function getSuccess() 
      { 
       ob_start(); 
      ?> 
      <script> 
       alert('Report Updated'); 
       window.location.href = 'index.php'; 
      </script> 
      <?php 
       $data = ob_get_contents(); 
       ob_end_clean(); 

       return $data; 
      } 
    } 

Upload.php

これは、このクラスは、あなたが証明されているものに基づいてどのように見えるかの単なる推測ですあなたの例では。

class Upload 
    { 
     public $uploaded, 
       $file_new_name_body, 
       $processed, 
       $error; 

     protected $filename, 
        $fileArray; 

     public function __construct($fileArray) 
      { 
       $this->fileArray = $fileArray; 
      } 

     public function process($path) 
      { 
       if(!is_dir($path)) 
        mkdir($path,0755,true); 

       if(empty($this->file_new_name_body)) 
        $this->file_new_name_body = $this->fileArray['name']; 

       $this->error  = $this->fileArray['error']; 
       $this->processed = move_uploaded_file($this->fileArray['tmp_name'],str_replace('//','/',$path.'/'.$this->file_new_name_body)); 

       if(!$this->processed) 
        throw new \Exception("File was unable to upload."); 
      } 
    } 

PowerUpload.php

/* 
** @description If you can, I would extend the Upload class and save some more readable methods to it 
*/ 
class PowerUpload extends Upload 
    { 
     public function setNewFile($fileArray) 
      { 
       $this->fileArray = $fileArray; 
       return $this; 
      } 

     public function setFileName($name) 
      { 
       $this->file_new_name_body = $name; 

       return $this; 
      } 

     public function checkHasUploaded() 
      { 
       $files = (!empty($this->fileArray)); 

       if(!$files) 
        throw new \Exception("File array can not be empty."); 

       return $files; 
      } 

     public function hasProcessed() 
      { 
       return $this->processed; 
      } 

     public function processUpload($path) 
      { 
       $this->process($path); 
      } 

     public function getError() 
      { 
       return $this->error; 
      } 
    } 

変化に基づいて、変更後のスクリプト:注あなたは簡単に名前のファイルを一致させることができるようにフォームの入力名が実際のキーの値を持っていること。

# Create the upload observer, $con is your database connection 
$Uploader  = new UploadObserver($con); 
# If there are files uploading 
if($Uploader->filesSet()) { 
    # Create a loop from the new array 
    # Keep the $key value to match it up with the files value 
    foreach($Uploader->getFiles() as $key => $file) { 
     # Use a try because the uploader submits exceptions on failures 
     try { 
      # Set the name 
      $newname = $t_timestamp."_".$key; 
      # Tag on the extension 
      $filename = $newname.".jpg"; 
      # Create the upload instance 
      $handle  = $Uploader->upload($file); 
      # This will throw exception if failed 
      $handle->checkHasUploaded(); 
      # Assign the file name and upload the file to folder 
      $handle->setFileName($newname)->processUpload(realpath(__DIR__.'/..').'/uploaded/'); 
      # Save the file info to your database using the connection passed originally 
      # You should throw exception here on database fail, PDO does it automatically so it's caught 
      $Uploader->saveToDb(array($_POST['photo_title'][$key],$filename,$aid)); 
      # Create javascript view 
      echo $Uploader->getSuccess(); 
     } 
     # Catch error(s) from the non-db classes  
     catch(\Exception $e) { 
      echo 'Error: '.$e->getMessage(); 
     } 
     # Catch error(s) from the db classes 
     catch(\PDOException $e) { 
      echo 'Error: '.$e->getMessage(); 
     } 
    } 
} 
?> 
<form method="post" enctype="multipart/form-data" action="<?php echo $this->getDataNode('_SERVER')->REQUEST_URI ?>"> 
    <table> 
     <tr> 
      <td><input class="form-control" id="customFieldValue2" name="photo_title[1]" type="text"></td> 
      <td><input class="form-control" id="customFieldName2" name="image_field[1]" type="file"></td> 
     </tr> 
     <tr> 
      <td><input class="form-control" id="customFieldValue2" name="photo_title[2]" type="text"></td> 
      <td><input class="form-control" id="customFieldName2" name="image_field[2]" type="file"></td> 
     </tr> 
    </table> 
    <input type="submit" value="SAVE" /> 
</form> 
+0

私はコードを編集しました。今はループが1つ少なくなります。しかし、私はまだ$ _POST ['photo_title']の配列を作成するのには無頓着です。 – musaspot

関連する問題