/** * This init method move the uploaded file to the target directory. * * @param array $args request arguments ($_REQUEST) */ public function init(&$args) { // Is the file upload from HTTP $requireUploadMove = false; $uploadedFile = $this->_findUploadedFile($this->name, $requireUploadMove); if (!$uploadedFile) { // Try to load uploadedFile from sourceField if ($this->sourceField) { $uploadedFile = $this->_findUploadedFile($this->sourceField, $requireUploadMove); } } if (!$uploadedFile) { return; } if ($uploadedFile->hasError()) { return; } $origFilename = $uploadedFile->getOriginalFileName(); if (!$origFilename) { error_log('ImageParam: source field file not found'); return; } $targetPath = trim($this->putIn, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $origFilename; if ($this->renameFile) { if ($ret = call_user_func($this->renameFile, $targetPath, $uploadedFile)) { $targetPath = $ret; } } // TODO: improve the file rename approach $renameLimit = 10; while ($targetPath && file_exists($targetPath) && $renameLimit--) { if ($a = Utils::filename_increase_suffix_number($targetPath)) { $targetPath = $a; } } // If there is a file uploaded from HTTP if ($requireUploadMove) { // The file array might be created from file system if ($savedPath = $uploadedFile->getSavedPath()) { copy($savedPath, $targetPath); } else { if ($uploadedFile->isUploadedFile()) { // move calls move_uploaded_file, which is only available for files uploaded from HTTP $uploadedFile->move($targetPath); } else { // is not an uploaded file? // may happen error here $uploadedFile->copy($targetPath); } } $this->action->saveUploadedFile($this->name, 0, $uploadedFile); } else { if ($this->sourceField) { // If there is no http upload, copy the file from source field // source field only works for update record action // skip updating from source field if it's a update action if ($this->action instanceof UpdateRecordAction) { return; } // Copy the file directly from the moved file path if ($savedPath = $uploadedFile->getSavedPath()) { copy($savedPath, $targetPath); } else { $uploadedFile->copy($targetPath); } $this->action->saveUploadedFile($this->name, 0, $uploadedFile); } else { return; } } // Update field path from target path // // argumentPostFilter is used for processing the value before inserting the data into database. if ($this->argumentPostFilter) { $a = call_user_func($this->argumentPostFilter, $targetPath); $args[$this->name] = $a; $this->action->args[$this->name] = $a; // for source field } else { $args[$this->name] = $targetPath; $this->action->args[$this->name] = $targetPath; // for source field } $this->action->addData($this->name, $targetPath); // Don't resize gif files, gd doesn't support file resize with animation if ($uploadedFile->getExtension() != 'gif') { $this->autoResizeFile($targetPath); } }
public function runWithRequest(ActionRequest $request) { if (!$request->getActionName()) { throw new InvalidActionNameException(""); } if (!Utils::validateActionName($request->getActionName())) { throw new InvalidActionNameException("Invalid action name: " . $request->getActionName() . "."); } return $this->run($request->getActionName(), $request->getArguments(), $request); }
function duplicate_file($from) { $to = Utils::filename_increase_suffix_number($from); copy($from, $to); return $to; }