1

jquery スクリプトからファイル名を取得する際に問題が発生しています。

フォームのファイル入力からのファイル名を含む複数の非表示フィールドがあり、これを使用してファイル名を取得します。

var fn = $('input[name="filename[]"]').serializeArray();
var post_var = {'filename':fn};

その後:

return JSON.stringify({
  "filename": post_var
});

それは私に次のようなものを与えます:

[Object { name="filename[]", value="703640495-qr-flo.png"}, Object { name="filename[]", value="703640495-qr-pgl.png"}]

しかし、次のような現在のphpスクリプトで「値」のコンテンツを取得する方法がわかりません。

 foreach($filename as $key => $value) {
    $imgrow = $this->db->dbh->prepare('INSERT INTO '. $this->config->db_prefix .'_images (aid, image) VALUES (:aid, :image)');
    $imgrow->bindValue(':aid', $id);
    $imgrow->bindParam(':image', strtolower($value));
    $imgrow->execute();

}

var_dump($filename) の場合、次のようになります。

array(1) {
  [0]=>
  object(stdClass)#104 (1) {
    ["filename"]=>
    array(2) {
      [0]=>
      object(stdClass)#105 (2) {
        ["name"]=>
        string(10) "filename[]"
        ["value"]=>
        string(20) "703640495-qr-flo.png"
      }
      [1]=>
      object(stdClass)#106 (2) {
        ["name"]=>
        string(10) "filename[]"
        ["value"]=>
        string(20) "703640495-qr-pgl.png"
      }
    }
  }
}  

解決:

foreach(array_shift($filename) as $file ) {
   foreach ($file as $key => $value) {
      $imgrow = $this->db->dbh->prepare('INSERT INTO '. $this->config->db_prefix .'_images (aid, image) VALUES (:aid, :image)');
         $imgrow->bindValue(':aid', $id);
         $imgrow->bindParam(':image', strtolower($value->value));
         $imgrow->execute();
   }
}  
4

2 に答える 2

1

ファイルは次の場所にある$filename[0]['filename']ため、次のことができます。

  1. $filenameにある配列を返す変数を配列シフトします$filename[0]['filename']
  2. 次に、返された配列をループします。各ループの反復により、名前と値のキーを含む配列が提供されます。

そのようです:

foreach( array_shift($filename) as $file ) {

   $file['name']; // the file name (always filename[] so ignore it)
   $file['value']; //the file value (the real filename)

}
于 2013-06-13T11:50:21.047 に答える