0

This is basic function for file upload. I need it to return a string which contains all image names, and if there are no images to upload to return FALSE. How can I do this?

function img_upload($folder) {
            $this->path = './public/img/' . $folder;
            $imgs = array();
            $g = true;
            $config = array(
                'allowed_types' => 'jpg|jpeg|png|gif',
                'upload_path' => $this->path
            );
            $CI = &get_instance();
            $CI->load->library('upload', $config);
            foreach ($_FILES as $key => $value) {

                if (!$CI->upload->do_upload($key)) {
                    return FALSE;
                } else {
                    $q = $CI->upload->data();
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $this->path . '/' . $q['file_name'];
                    $config['new_image'] = $this->path . '/thumbs';
                    $config['create_thumb'] = FALSE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 128;
                    $config['height'] = 128;

                    $CI->load->library('image_lib', $config);
                    $CI->image_lib->resize();
                    echo $q['file_name'];                
                    $imgs = array_push($imgs, $q['file_name']);
                }      
            }        
        }

1 Answer 1

1

If i understand you correct than this code may help you..

 function img_upload($folder) {
            $this->path = './public/img/' . $folder;
            $imgs = array();
            $g = true;
            $config = array(
                'allowed_types' => 'jpg|jpeg|png|gif',
                'upload_path' => $this->path
            );
            $CI = &get_instance();
            $CI->load->library('upload', $config);
            foreach ($_FILES as $key => $value) {

                if (!$CI->upload->do_upload($key)) {
                    return FALSE;
            } else {
                $q = $CI->upload->data();
                $config['image_library'] = 'gd2';
                $config['source_image'] = $this->path . '/' . $q['file_name'];
                $allimages = array();
                $allimages = $q['file_name'];
                $config['new_image'] = $this->path . '/thumbs';
                $config['create_thumb'] = FALSE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 128;
                $config['height'] = 128;

                $CI->load->library('image_lib', $config);
                $CI->image_lib->resize();
                echo $q['file_name'];                
                $imgs = array_push($imgs, $q['file_name']);


                 if(count($allimages) > 0)
                   {
                     return $allimagename = ('',$allimages);
                   }
                 else
                   {
                     return 'Thre is no images';
                    }
                }      
            }        
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.