user0129e021939232
user0129e021939232

Reputation: 6355

CakePHP 2.0 File Upload, $data has no effect

Im working on a site that will enable me to upload files to the server, Im trying to get the file to be renamed to the corresponding mysql id that is inserted with the information.

It kind of works, but every time I upload a file it will re-write over the last file that was uploaded.

This is my code

   function uploadFile() {
    $file = $this->data['Upload']['file'];
    $pid = mysql_insert_id();
    if ($file['error'] === UPLOAD_ERR_OK) {
        if (move_uploaded_file($file['tmp_name'], APP.'webroot/files/uploads'.DS."$pid.mp4")) {
            $this->data['Upload']['name'] = $file['name'];
            $this->data['Upload']['eventname'] = $file['evetname'];
            $this->data['Upload']['description'] = $file['description'];
            return true;
        }
    }
    return false;
}

These are the errors that are occuring on my site,

Notice (8): Indirect modification of overloaded property UploadsController::$data has no effect [APP/Controller/uploads_controller.php, line 58] Notice (8): Undefined index: eventname [APP/Controller/uploads_controller.php, line 59] Notice (8): Indirect modification of overloaded property UploadsController::$data has no effect [APP/Controller/uploads_controller.php, line 59] Notice (8): Undefined index: description [APP/Controller/uploads_controller.php, line 60] Notice (8): Indirect modification of overloaded property UploadsController::$data has no effect [APP/Controller/uploads_controller.php, line 60]

I don't quite get what is going on really? Also should I be using the mysqli_insert_id() function instead of mysql_insert_id()? I don't really know how the mysqli_insert_id() works, any help please guys???

Upvotes: 2

Views: 2742

Answers (1)

mensch
mensch

Reputation: 4411

You're trying to manipulate $this->data which is read only in Cake 2.0. You should use the new CakeRequest object introduced in Cake 2.0.

So $this->data becomes: $this->request->data.

As for mysql_insert_id(), I'm not sure where you are saving data to a database table. Cake stores the id of the last inserted record in $this->ModelName->id, so you could use that as well.

Upvotes: 6

Related Questions