Move file from public directory in some other in drupal 7

To move file we need to have file object. We can get it using file_load(fid) function wich need file id as argumnet

$file = file_load(fid);
if(isset($file)){
 $date = date('d-m-Y');
 //here we get any node we need
 $node = node_load(1);
          //here we actually move file
          @$file = file_move($file, 'public://webform/' . $date . '/' . $node->nid, $replace = FILE_EXISTS_RENAME);
          // here we can add this file already moved to any node field with type file
          if(!empty($file)){
            $node->field_file_downloads = array("und" => array(0 => array("value" => $file->uri)));
            node_save($node);
          }
        }
Rating: 
10 out of 10 based on 1 ratings.