0

I want to upload image using cakephp and i am able to save file name in database but my file file is not uploading in specified path ihave tried the below code but my problem has not solved

This is my controller

function editprofile ($id = NULL){
                if(empty($this->data)){
                    $this->data=$this->Signup->read(NULL, $id);
                }
                else{
              $image = $this->data['Signup']['upload image'];
                //allowed image types
                $imageTypes = array("image/gif", "image/jpeg", "image/png");
                //upload folder - make sure to create one in webroot
                $uploadFolder = "upload";
                //full path to upload folder
                $uploadPath = WWW_ROOT . $uploadFolder;
 $imageName = $image['name'];
 if($image['size'] > 2097152){
            $errors[]='File size must be less than 2 MB';
        }   
                //check if image type fits one of allowed types
                if(empty($errors)==true){
            if(is_dir($uploadPath)==false){
                mkdir("$uploadPath", 0700);     // Create directory if it does not exist
            }
            if(is_dir("$uploadPath/".$imageName)==false){
                move_uploaded_file($image['tmp_name'],"$uploadPath/".$imageName);
            }else{                                  // rename the file if another one exist
                $new_dir="$uploadPath/".$imageName.time();
                 rename($image['tmp_name'],$new_dir) ;              
            }

        }

                    if ($this->Signup->save($this->data)) {
                        $this->Session->setFlash($imageName);
                        return $this->redirect(array('action' => 'editprofile',$id));
                    }


                }
                $this->set('languageOptions', array('opt1' => 'Choose Occupuation', 'opt2' => 'Student', 'opt3' => 'Employee'));
    debug($this->Signup->validationErrors);
    debug($this->Signup->getDataSource()->getLog(false, false));
                 //$this->set('title_for_layout', 'Add a post ');
        }

This is my view

echo $this->Form->input('upload image',array( 'type' => 'file' , 'style' => 'margin-left:9%;'));
2
  • I will recommend you to use the Uploader plugin from milesj.me/code/cakephp/uploader . It helps you save a lot of coding. Commented Jun 26, 2014 at 8:45
  • I would recommend my own plugin ;) github.com/burzum/cakephp-file-storage It's not only an upload plugin but a complete solution to file handling. Because what I see in this question is a prime example of how NOT to organize your files and meta data. Commented Jun 26, 2014 at 12:01

1 Answer 1

1

I've run into this issue several times myself, and most often the problem is caused by a detail that I keep forgetting: setting the right enctype for the form. Based on your code above, I can't see any obvious mistakes, but I'm not able to see if the enctype is set to multipart/form-data or not. Try this when you create the form in the view (note the 'type' => 'file'):

echo $this->Form->create('FormName', array('type' => 'file'));
Sign up to request clarification or add additional context in comments.

1 Comment

I recommend this pretty good tutorial on uploading multiple files at the same time. unselfishideas.blogspot.com/2014/08/…

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.