Jam Dvia
Jam Dvia

Reputation: 27

CodeIgniter upload one image to two directory

I would like to upload one image to different directory in CodeIgniter, so one images are stored in 2 folders.

Path one etc/www/image1/ and path two etc/www/image2/

Code

$config[‘upload_path’] =‘etc/www/image1/’;
$config[‘allowed_types’] = ‘jpg|jpeg|gif|png’;
$config[‘file_name’]=“imageone.jpg”;
$config[‘max_size’] = ‘10000’;

$this->upload->initialize($config); 
if(!$this->upload->do_upload(‘userfile’)){
echo $this->upload->display_errors();
}else {
$this->upload->data(‘userfile’);
} 

Upvotes: 0

Views: 3908

Answers (2)

A. Deliaslan
A. Deliaslan

Reputation: 37

 $config['upload_path'] = FCPATH.'upload/';
    $config['allowed_types'] = FCPATH.'gif|jpg|jpeg|png';
    $config['encrypt_name'] = TRUE;
    $this->load->library('upload',$config);
    $upload = $this->upload->do_upload("resim");
    if($upload){
        $resim = $this->upload->data();
        $resimadi = $resim['file_name'];
        $resimkayit = 'upload/'.$resimyolu.'';
        $resimhotnews = 'upload/hotnews'.$resimadi.'';
        $resimlastnews = 'upload/lastnews'.$resimadi.'';
        $config['image_library'] = 'gd2';
        $config['source_image'] = 'upload/'.$resimadi.'';
        $config['new_image'] = 'upload/hotnews'.$resimadi.'';
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = FALSE;
        $config['quality'] = '%80';
        $config['width'] = 500;
        $config['height'] = 350;

        $this->load->library('image_lib', $config);
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        $this->image_lib->clear();
        //tmb folder resim uploading end

//mini folder resim uploading start

        $config1['image_library'] = 'gd2';
        $config1['source_image'] = 'upload/'.$resimadi.'';
        $config1['new_image'] = 'upload/lastnews/'.$resimadi.'';
        $config1['create_thumb'] = FALSE;
        $config1['maintain_ratio'] = FALSE;
        $config1['quality'] = '%80';
        $config1['width'] = 200;
        $config1['height'] = 150;

        $this->load->library('image_lib', $config1);
        $this->image_lib->initialize($config1);
        $this->image_lib->resize();
        $this->image_lib->clear();

//on mac computers you have to give a writable permission for the folders

Upvotes: 0

TigerTiger
TigerTiger

Reputation: 10806

Just use PHP's Copy() function after you have uploaded the file successfully in your CI controller method...

EX:

$file = '/www/image1/example.txt';
$newfile = '/www/image1/example.txt';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}

Upvotes: 2

Related Questions