0

I'm completely confused on this. I'm trying to upload an image but I keep getting this error, I've searched the site and I've found tons of the same question but I couldn't get any of those solutions to work for me.

I have this code in my model that is supposed to convert the image array to a string.

     public function processImageUpload($check = array()) {
          if (!is_uploaded_file($check['image']['tmp_name'])){
         return FALSE;
         }
         if (!move_uploaded_file($check['image']['tmp_name'], WWW_ROOT .
             'img' . DS . 'uploads' . DS . $check['image']['name'])){
          return FALSE;
          }
             $this->data[$this->alias]['image'] = 'uploads' . DS . 
              $check['image']['name'];
          return TRUE;
     }

Next up I have my add function from PostsController

       public function add() {

        if($this->request->is('post')) {
          $this->request->data['Post']['username']= $this->Auth->user('username');
          $this->Post->create();
          $data = $this->request->data['Post'];
        if (!$data['image']['name']){
          unset($data['image']);
          }
        if($this->Post->save($data)) {
          $this->Session->setFlash(__('Your post has been saved.'));
            return $this->redirect(array('action'=>'index'));
            }
          $this->Session->setFlash(__('Unable to add your post.'));
             }
     }

Then I have my form where the file is uploaded.

echo $this->Form->create('Post', array('type' => 'file'));
echo $this->Form->input('title');
echo $this->Form->input('movie');
echo $this->Form->input('category');
echo $this->Form->input('description' , array('rows'=>'2'));
echo $this->Form->input('image', array('type' => 'file'));
echo $this->Form->end('Submit Article for Review');

So why is the function in my model not converting the array into a string? What am I not understanding here?

1 Answer 1

1

Because it was not supposed to.

What the function move_uploaded_file is doing is uploading your file to the server and setting the database value as its PATH, not converting to a binary file in your database.

Sign up to request clarification or add additional context in comments.

1 Comment

Alright, I thought that $this->data[$this->alias]['image'] = 'uploads' . DS . $check['image']['name']; was setting ['Post']['image'] equal to the path of the file. What do I need to do to get the array to a string in this case?

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.