2014-03-27 2 views
0

우선 저는 여러분 께 기여해 주신 모든 분들께 감사 드리며 가능한 한 많은 도움을 드리고자합니다.단일 이미지로 여러 이미지 업로드

이제 저는 많은 것을 시도해 보았습니다 만, 큰 문제는 아니지만 다른 방법으로 시도해도 해결 방법을 찾지 못했습니다.

그래서 자세히 설명해 드리겠습니다. id가 | typology_id | pic_path 인 typology_pictures 테이블이 있습니다. 사용자가이처럼 보여주는 새로운 typology_picture을 만들려고

:

Adding Images For a chosen Typology

그래서 사용자가 유형학을 선택 이들 사진 belonges에 그가 눈썹 때, 나는 그가 선택할 수 있도록하려면 다중 이미지. 그리고 submit 버튼을 누르면 데이터베이스에 삽입 될 모든 이미지가 새로운 행에 삽입되지만 typology_id은 동일합니다. 단지 pic_path가 다릅니다. 추가 조치에 대한

<?php 
App::uses('AppModel', 'Model'); 
/** 
* TypologyPicture Model 
* 
* @property Typology $Typology 
*/ 
class TypologyPicture extends AppModel { 
public $name = 'TypologyPicture'; 

    public $primaryKey = 'id'; 

    public $displayField = 'typology_id'; 


    public $validate = array(
     'id' => array(
      'blank' => array(
       'rule' => 'blank', 
       'on' => 'create', 
      ), 
     ), 
     'typology_id' => array(
      'numeric' => array(
       'rule' => array('numeric'), 
       'message' => 'Chose the Typology this pic belongs to?', 
      ), 
      'notEmpty' => array(
       'rule' => array('notEmpty'), 
       'message' => 'Select The typology where these picture belongs to? ', 
       'on' => 'create', 
      ), 
     ), 
     'pic_path' => array(
      'uploadError' => array(
       'rule' => 'uploadError', 
       'message' => 'The Typology Image upload failed.', 
       'allowEmpty'=>false, 
       'on' => 'create', 
      ), 
      'fileSize' => array(
       'rule' => array('fileSize', '<=', '3MB'), 
       'message' => 'Cover image must be less than 3MB.', 
       'allowEmpty'=>false, 
       'on' => 'create', 

      ), 
      'processTypologyImageUpload' => array(
       'rule' => 'processTypologyImageUpload', 
       'message' => 'Unable to process cover image upload.', 
       'allowEmpty' => TRUE, 
      ), 
     ), 
    ); 

    //The Associations below have been created with all possible keys, those that are not needed can be removed 

/** 
* belongsTo associations 
* 
* @var array 
*/ 
    public $belongsTo = array(
     'ItemTypologyPicture' => array(
      'className' => 'Typology', 
      'foreignKey' => 'typology_id', 
      'conditions' => '', 
      'fields' => '', 
      'order' => '' 
     ) 
    ); 


     /*CopyRight @Erland Muchasaj*/ 
    // This new function Doesent Upload the Picture or Image unless the Record is inserted in DB. 
    public function processTypologyImageUpload($checktypology = array()) { 
     $dir = 'img' . DS . 'uploads' . DS . 'typology' . DS . 'images' . DS; 
      if (!isset($checktypology['pic_path']['name'])) { 
       $this->invalidate('pic_path', 'You must upload a file before you Submit!'); 
       return false; 
      } 

      if(!is_uploaded_file($checktypology['pic_path']['tmp_name'])) { 
       $this->invalidate('pic_path', 'You must upload a file before you Submit!'); 
       return false; 
      } 

      if (($checktypology['pic_path']['size'] > 55000000)) { 
       $this->invalidate('pic_path', 'File size is To big!'); 
       return false; 
      } 
       $allowedExts = array('jpeg', 'png', 'jpg', 'gif','tiff'); 
       $value = explode(".", $checktypology['pic_path']['name']); // Here we grab the name of the file with the extension 
       $extension = strtolower(array_pop($value)); //extension 
       // the file name is before the last "." 
       $fileName = array_shift($value); //Filename       
      //$allowedExts = array('jpeg', 'png', 'jpg', 'gif','tiff'); 
      //$extension=strtolower(end(explode(".", $checktypology['pic_path']['name']))); 
      if(!in_array($extension, $allowedExts)){ 
       $this->invalidate('pic_path', 'Invalid File Format! '); 
       return false; 
      } 

      if ($checktypology['pic_path']["error"] > 0) { 
       $this->invalidate('pic_path', $checktypology['pic_path']['error']); 
       return false; 
      } 

      if (file_exists(WWW_ROOT . $dir) && is_dir(WWW_ROOT . $dir)) { 
       chmod(WWW_ROOT . $dir, 0777); 
       if (file_exists(WWW_ROOT . $dir . $checktypology['pic_path']['name'])) { 
        $this->invalidate('pic_path', 'File Allredy Exists!'); 
        return false;  
       } 
      } 

      if (!file_exists(WWW_ROOT . $dir) || !is_dir(WWW_ROOT . $dir)) { 
       mkdir($dir, 0777, true); 
      }    
    // better safe then sorry! 
    return true; 
    } 


    // this is a Global variable that im gonna use it inside my function 
     private $image; 
     public function beforeSave($options = Array()) { 
      $dir = 'img' . DS . 'uploads' . DS . 'typology' . DS . 'images' . DS; 
      // first we grab slider id, and we pass it's data the the variable. 
      $this->image = $this->find('first', array('conditions' => array('TypologyPicture.id' => $this->id))); 

       if (isset($this->data[$this->alias]['pic_path']) && !empty($this->data[$this->alias]['pic_path'])) { 
        $file = $this->data[$this->alias]['pic_path']; 
         if (!move_uploaded_file($file['tmp_name'], WWW_ROOT . $dir . time() .$file['name'])) { 
          $this->invalidate('pic_path', 'File Didnt Upload! '); 
          return FALSE; 
         } 
        $this->data[$this->alias]['pic_path'] = time() . $file['name']; 
       return TRUE; 
       }  
      return true; 
     } 

     public function afterSave($created, $options = Array()) { 
      //$dir = 'img/uploads/typology/images/'; /*<=== DO NOT EDIT*/ 
      $dir = 'img' . DS . 'uploads' . DS . 'typology' . DS . 'images' . DS; 
      // then after deletation of the row we check if the file exist, if so we delete it. 
      if(isset($this->data[$this->alias]['pic_path']) && file_exists(WWW_ROOT . $dir . $this->image['TypologyPicture']['pic_path'])){ 
      chmod(WWW_ROOT . $dir, 0777); 
       $img = WWW_ROOT . $dir . $this->image ['TypologyPicture']['pic_path']; 
       unlink($img); 
       return true; 
      } else { $this->data[$this->alias]['pic_path'] = $this->image['TypologyPicture']['pic_path'];} 
      return true; 
     } 


     // this is a Global variable that im gonna use it inside my function 
     private $TypologyPicture; 
     public function beforeDelete($cascade = true) { 
      // first we grab slider id, and we pass it's data the the variable. 
      $this->TypologyPicture = $this->find('first', array('conditions' => array('TypologyPicture.id' => $this->id))); 
      return true; 
     } 

     public function afterDelete() { 
     //$dir = 'img/uploads/typology/images/'; /*<=== DO NOT EDIT*/ 
     $dir = 'img' . DS . 'uploads' . DS . 'typology' . DS . 'images' . DS; 
     // then after deletation of the row we check if the file exist, if so we delete it. 
      if(file_exists(WWW_ROOT . $dir . $this->TypologyPicture['TypologyPicture']['pic_path'])){ 
      chmod(WWW_ROOT . $dir, 0777); 
       $img = WWW_ROOT . $dir . $this->TypologyPicture ['TypologyPicture']['pic_path']; 
       unlink($img); 
       return true; 
      } 
      return true; 
     } 


} 

컨트롤러 :

그래서 모델이있다

public function add() { 
    $this->layout='typology_add'; 
    if ($this->request->is('post')) {  
     if (!empty($this->data)) { 

      $this->TypologyPicture->create(); 
      if(empty($this->data['TypologyPicture']['pic_path']['name'])){ 
       unset($this->request->data['TypologyPicture']['pic_path']); 
       } 
      if ($this->TypologyPicture->saveAll($this->data)) 
      {   
       $this->Session->setFlash(__('The profile has been saved', true)); 
       $this->redirect(array('action' => 'index')); 
      } else 
      { 
       $this->Session->setFlash(__('The profile could not be saved. Please, try again.', true)); 
      } // END OF TypologyPicture->save 


     } // END OF THE !empty();  
    if (empty($this->data)) { 
      $this->data = $this->TypologyPicture->read(null, $id); 
     }   
    } 


     $typologies = $this->TypologyPicture->ItemTypologyPicture->find('list'); 
    $this->set(compact('typologies')); 
} 

그리고보기는 이것이다 :

<?php echo $this->Form->create('TypologyPicture', array('type'=>'file')); ?> 
    <legend><?php echo __('Add Typology Pictures'); ?></legend> 
    <?php 
    echo $this->Form->input('id'); 
    echo $this->Form->input('typology_id'); 
    echo $this->Form->input('pic_path', array('label'=>'Picture','type'=>'file')); 
    ?> 
<?php echo $this->Form->end(__('Submit')); ?> 

난 정말겠습니까 너를 애타게해라. lp.

고맙습니다.

편집 :

이 내가 지금까지 writen 한 코드입니다! 다중 파일 지원 선택하기 위해

echo $this->Form->input('pic_path.', array('label'=>'Picture','multiple'=>'multiple' ,'type'=>'file')); 

:

public function add() { 
    if ($this->request->is('post')) {  
     $dir = 'img' . DS . 'uploads' . DS . 'typology' . DS . 'images' . DS; 
     for($i=0;$i<count($this->request->data['TypologyPicture']['pic_path']['name']);$i++){ 
      if(empty($this->data['TypologyPicture']['pic_path'][$i]['name'])) { 
       unset($this->request->data['TypologyPicture']['pic_path'][$i]['name']); 
      } 
      if(!empty($this->request->data['TypologyPicture']['pic_path'][$i]['tmp_name'])){  
       $allowedExts = array('jpeg', 'png', 'jpg', 'gif','tiff'); 
       $value = explode(".", $this->data['TypologyPicture']['pic_path'][$i]['name']); // Here we grab the name of the file with the extension 
       $extension = strtolower(array_pop($value)); //extension 
       if(in_array($extension, $allowedExts)){ 
        if ($this->data['TypologyPicture']['pic_path'][$i]['error'] <= 0) { 
         chmod(WWW_ROOT . $dir, 0777); 
         move_uploaded_file($this->data['TypologyPicture']['pic_path'][$i]['tmp_name'], WWW_ROOT . $dir . mktime().$this->data['TypologyPicture']['pic_path'][$i]['name']); 
         $this->request->data['TypologyPicture']['pic_path'] = mktime().$this->data['TypologyPicture']['pic_path'][$i]['name']; 
        } 
       } 
      } 
      $this->TypologyPicture->create(); 
      if ($this->TypologyPicture->save($this->data)){   
       $this->Session->setFlash(__('The profile has been saved', true)); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The profile could not be saved. Please, try again.', true)); 
      } // END OF ItemPicture->save 
     } 
    }   
    $typologies = $this->TypologyPicture->ItemTypologyPicture->find('list'); 
    $this->set(compact('typologies')); 
} 

는 또한 입력 필드로 변경.

요청이왔다 검은 전진 오류 : 그러나 다시 아무것도 난 그냥 얻을 ..

을 작동하는 것 같다 요청 된 주소를 '/ cesifull/typology_pictures/추가'이 서버에서 찾을 수 없습니다.

제안 사항 ... 누구든지!

답변

0

하나의 파일 입력으로 여러 파일을 선택할 수 있다는 것은 HTML5 기능이므로 작성하는 모든 솔루션에는 HTML5가 포함될 가능성이 큽니다. HTML5를 사용하고 싶지 않으면 사람들은 여러 이미지 업로드를 위해 Flash 및 Java 플러그인을 만들었습니다.

http://verens.com/2009/12/28/multiple-file-uploads-using-html5/

+0

는만큼 내가 알고 있는데로, CakePHP는 그래서 그것은 다중 파일 업로드 – landi

+0

그것은의 지원 생각 HTML5를 사용하여 더 많은 브라우저가 이전과 같은 HTML5 기능을 지원하지 않기 때문에, 지원하려는 브라우저를 무엇의 문제 IE의 버전. 이것이 중요하지 않다면 HTML5 다중 파일 입력이 쉬운 방법입니다. – Kai

+0

글쎄, 구글 크롬과 파이어 폭스 같은 주요 브라우저가 그렇게하고 싶습니다. @ 카이를하는 방법을 아는 경우 실제로 큰 도움이 될 것입니다 ... – landi

관련 문제