/** * Moves an uploaded file from the temp directory to a permanent location * * @throws fValidationException When `$directory` is somehow invalid or ::validate() thows an exception * * @param string|fDirectory $directory The directory to upload the file to * @param string $field The file upload field to get the file from * @param mixed $index If the field was an array file upload field, upload the file corresponding to this index * @return fFile|NULL An fFile (or fImage) object, or `NULL` if no file was uploaded */ public function move($directory, $field, $index = NULL) { if (!is_object($directory)) { $directory = new fDirectory($directory); } if (!$directory->isWritable()) { throw new fProgrammerException('The directory specified, %s, is not writable', $directory->getPath()); } if (!self::check($field)) { throw new fProgrammerException('The field specified, %s, does not appear to be a file upload field', $field); } $file_array = $this->extractFileUploadArray($field, $index); $error = $this->validateField($file_array); if ($error) { throw new fValidationException($error); } // This will only ever be true if the file is optional if ($file_array['name'] == '' || $file_array['tmp_name'] == '' || $file_array['size'] == 0) { return NULL; } $file_name = fFilesystem::makeURLSafe($file_array['name']); $file_name = $directory->getPath() . $file_name; if (!$this->enable_overwrite) { $file_name = fFilesystem::makeUniqueName($file_name); } if (!move_uploaded_file($file_array['tmp_name'], $file_name)) { throw new fEnvironmentException('There was an error moving the uploaded file'); } if (!chmod($file_name, 0644)) { throw new fEnvironmentException('Unable to change permissions on the uploaded file'); } return fFilesystem::createObject($file_name); }
/** * Moves an uploaded file from the temp directory to a permanent location * * @throws fValidationException When `$directory` is somehow invalid or ::validate() thows an exception * * @param string|fDirectory $directory The directory to upload the file to * @param string $field The file upload field to get the file from * @param integer $index If the field was an array file upload field, upload the file corresponding to this index * @return fFile An fFile (or fImage) object */ public function move($directory, $field, $index = NULL, $param_filename = NULL) { if (!is_object($directory)) { $directory = new fDirectory($directory); } if (!$directory->isWritable()) { throw new fProgrammerException('The directory specified, %s, is not writable', $directory->getPath()); } if (!self::check($field)) { throw new fProgrammerException('The field specified, %s, does not appear to be a file upload field', $field); } $file_array = $this->validate($field, $index); $file_name = fFilesystem::makeURLSafe($param_filename == NULL ? $file_array['name'] : $param_filename); $file_name = $directory->getPath() . $file_name; if (!$this->enable_overwrite) { $file_name = fFilesystem::makeUniqueName($file_name); } if (!move_uploaded_file($file_array['tmp_name'], $file_name)) { throw new fEnvironmentException('There was an error moving the uploaded file'); } if (!chmod($file_name, 0644)) { throw new fEnvironmentException('Unable to change permissions on the uploaded file'); } return fFilesystem::createObject($file_name); }