-1

I am developing a ad site that anyone able to upload 3 images. So I am coding to upload that 3 images by adding timestamp in front of their file name to make them unique. I use codeigniter framework and attaching the code below.

when I submitting form data, localhost server shows that images have been saved correctly with some code infornt of image filename. But problem is there are no images saved in relevant image folder in that name and I cannot retrieve that images in next preview advertisement page. I don't know much about php or codeignitor. Your help is highly appreciated.

        $config['upload_path'] = './assets/images/adsimages';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 5120;

        $this->load->library('upload',$config);
        $this->upload->initialize($config);

        if (!$this->upload->do_upload()){
            $errors = array('error' => $this->upload->display_errors());
            $post_image = 'no_image.jpg';
        }
        else {
            $data = array('upload_data' => $this->upload->data());

            $post_image1 = time().$_FILES['userfile1']['name'];
            $post_image2 = time().$_FILES['userfile2']['name'];
            $post_image3 = time().$_FILES['userfile3']['name'];
        }         
        $result = $this->post_model->adregister($post_image1,$post_image2,$post_image3);

3 Answers3

0

You can append time to 'filename' in config of upload library

$config['file_name'] = time().$_FILES['userfile1']['name'];

Or if you want unique name for all files the simply add

$config['encrypt_name'] = TRUE;

then

$this->load->library('upload', $config);
-1

Try this one:-

$path = pathinfo($_FILES["userfile1"]["name"]);
$image_path = $path['filename'].'_'.time().'.'.$path['extension'];
KUMAR
  • 1,993
  • 2
  • 9
  • 26
-2

I've written a possible solution for your code. You haven't shared your full code so you'll have to fill in the gaps yourself and maybe make some changes here and there; Comments are mentioned wherever necessary. See if it helps you.

public function your_function_name(){
    // your-code
    // your-code

    // check if file is uploaded in field1
    if(!empty($_FILES['userfile1']['name'])){ 

        // call function to upload file
        $userfile1 = $this->upload_file('userfile1'); 
    }

    // check if file is uploaded in field2
    if(!empty($_FILES['userfile2']['name'])){ 

        $userfile2 = $this->upload_file('userfile2');
    }

    // check if file is uploaded in field3
    if(!empty($_FILES['userfile3']['name'])){ 

        $userfile3 = $this->upload_file('userfile3');
    }

    $result = $this->post_model->adregister($userfile1, $userfile2, $userfile3);
}

// function to upload file
function upload_file($filename){

    $config['file_name']     = time().$_FILES[$filename]['name']; // append time to filename
    $config['upload_path']   = './assets/images/adsimages';
    $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG';
    $config['max_size']      = 5120;

    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    $uploaded = $this->upload->do_upload($filename);

    if ( ! $uploaded ){

        $error = array('error' => $this->upload->display_errors());
        $file  = 'no_image.jpg'; // default file

    }else{

        $upload_data = $this->upload->data();
        $file        = $upload_data['file_name']; // uploaded file name
    }

    return $file; // return filename
}
sauhardnc
  • 1,961
  • 2
  • 6
  • 16