FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

moving files within filesystem #463

Closed anuj9196 closed 7 years ago

anuj9196 commented 7 years ago

As mentioned in documentation

It can also move files around programatically. Such as from the filesystem.

I'm moving files within filesystem.

I have to create a new record with same video file in a separate file just as newEntity does, but instead of uploading file, I have to move a pre-uploaded file withing filesystem

$newPostVideo = $this->PostVideos->newEntity();
$newPostVideo->video_file = WWW_ROOT . '..' . DS . $postVideo->dir . DS . $postVideo->video_file;
$newPostVideo = $this->PostVideos->patchEntity($newPostVideo, $this->request->getData());

A new record is created in database but video_file and dir fields are NULL.

Files are saved at

webroot/files/PostVideos/video_file/{timestamp_directory/file.mp4

and dir column has value

webroot/files/PostVideos/video_file/{timestamp_directory}
saeideng commented 7 years ago

Change place of line 2 with 3 ...patchEntity... line with top line

saeideng commented 7 years ago

And why you need to patchEntity?

anuj9196 commented 7 years ago

because there are some data which is coming from form. I just have to copy same file to a new path, rest data are from form.

anuj9196 commented 7 years ago

changed my code to look like

$path = WWW_ROOT . '..' . DS . $postVideo->dir . DS . $postVideo->video_file;
$newPostVideo = $this->PostVideos->newEntity([
         'video_file' => $path,
         'campaign_id' => $this->request->getData('campaign_id'),
]);

and changed my column to not null in database, which now gives error as

 Error: SQLSTATE[HY000]: General error: 1364 Field 'video_file' doesn't have a default value 

Same is working fine for direct uploading. but having problem here

saeideng commented 7 years ago

i dont know you can moving file by this plugin or no you can move your file and insert new record by custom code

transaction begin 
if(save new record ){
      if (move file)
          return true//commit 
      else 
        return false// roleback 
}
davidyell commented 7 years ago

I would use a listener here, and have that listen for the afterSave event, unless the plugin provides some event hooks. Then you can do your additional processing there, such as moving the file using copy() or rename()