CakePHP I-save ang isang pulutong ng mga larawan at display (Upload Multiple Image and Display it)

First, i am sorry about the messy writing. I just want to share my knowledge.

ako ay ipaliwanag kung paano i-save ang isang pulutong ng mga larawan mula sa computer at display. una sa lahat, kailangan namin ng isang database. ang lugar kung saan ang larawan ay nai-save.

sample istraktura ng talahanayan 'images':

CREATE TABLE `images` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=903 DEFAULT CHARSET=latin1;

   
maghain View, i-type ang sumusunod na code:

<?php
echo ($this->Form->create('Image', array('name' => 'NormalImport', 'url' => array('controller' => 'uploads', 'action' => 'upload_file')));  
echo $this->Form->input('user_image.',array('type'=>'file','label' => false,'placeholder' => 'Upload images','id'=>"inputFile",'multiple','onchange'=>'readURL(this)'));?> 
echo($this->Form->submit('Upload File', array('div'=>false, 'class'=>'btn btn-primary pull-left')));
echo($this->Form->end());
?>  


ugis o istraktura ng naka-save na larawan ang magiging hitsura tulad ng sumusunod. Ang mas maraming mga larawan na ia-upload, ang bilang ng array ay dagdagan.

Array
    (
        [Model] => Array
            (
                [files] => Array
                    (
                        [0] => Array
                            (
                                [name] => MY.jpg
                                [type] => image/jpeg
                                [tmp_name] => // temp path
                                [error] => 0
                                [size] => 1826587
                            )
                        [1] => Array
                            (
                                [name] => YOU.jpg
                                [type] => image/jpeg
                                [tmp_name] => // temp path
                                [error] => 0
                                [size] => 1127346
                            )
            )
    )


  
Kaya upang i-save ang mga imahe lamang gumawa ng isang looping function na bilang ang napiling imahe. Sa iyong controller file type ang sumusunod na code:

public function admin_upload_file($quiz_id = null, $type = null, $user_id = null)
{
$_file    =    $this->request->data['Image']['user_image'];
$n = count($this->request->data['Image']['user_image']); 
if(!empty($this->data['Image']['user_image']))
{        
for($i= 0;$i < $n; $i++) {  
$file=$this->request->data['Image']['user_image'];
$ary_ext=array('jpg','jpeg','png','PNG','JPG','JPEG');  
$ext = substr(strtolower(strrchr($file[$i]['name'], '.')), 1); if(in_array($ext, $ary_ext)) {  
move_uploaded_file($file[$i]['tmp_name'], WWW_ROOT . IMAGE_FILE_FULL_DIR . DS . time().$file[$i]['name']);
$name = time().$file[$i]['name'];
$name_arr[$i] = $name;
$imagePath    = IMAGE_PATH_FOR_TIM_THUMB.'/'.IMAGE_FILE_FULL_DIR.'/'; 
$imageData['Image']['name'] = $imagePath.$name; $this->Image->create();
$this->Image->save($imageData);            
}
}  
if(in_array($ext, $ary_ext))
{            
$this->Session->setFlash("File Imported successfully", 'admin_flash_good');
}else
{
$this->Session->setFlash(__('Please upload an image file.', true), 'admin_flash_bad');
}
}
else
{
$this->Session->setFlash(__('Please upload an image file.', true), 'admin_flash_bad');
}
}

 
Upang magpakita ng mga larawan, gamitin ang img src:

<?php 
echo "<img src='".$value['Image']['name']."'' width='200px' height='70px'></img>";
?>

Comments

Popular Posts