Example #1
0
 /**
  * In this method, we don't modify tmp_name attribute
  * rather than that, we set the saved_path attribute
  * for location of these moved files.
  */
 public function putIn($targetDir, $targetFileName = null, $useCopy = false)
 {
     /* source file */
     $file = $this->tmp_name;
     if (!file_exists($file) && isset($_FILES[$this->column]['saved_path'])) {
         $useCopy = true;
         $file = $_FILES[$this->column]['saved_path'];
     }
     // if targetFilename is not given,
     // we should take the filename from original filename by using basename.
     if (!$targetFileName) {
         $targetFileName = basename($this->name);
     }
     // make sure we have the directory exists.
     if (!file_exists($targetDir)) {
         FileUtils::mkpath($targetDir);
     }
     // relative file path.
     $newPath = FileUtils::path_join($targetDir, $targetFileName);
     /* avoid file name duplication */
     $fileCnt = 1;
     while (file_exists($newPath)) {
         $newPath = FileUtils::path_join($targetDir, FileUtils::filename_suffix($targetFileName, '_' . $fileCnt++));
         // substr(md5_file( $file ),0,5) . '_' . $targetFileName );
     }
     /* register to $_FILES[ name ][ savedpath ]
      *
      * in CRUD action, we need to validate if a action file column's value
      * is a real upload file.
      * */
     $this->saved_path = $newPath;
     if ($useCopy) {
         copy($file, $newPath);
     } else {
         $this->move($file, $newPath);
     }
     $_FILES[$this->column]['saved_path'] = $newPath;
     return $newPath;
 }