class ResourceUpload in D7 Media 6
Hierarchy
- class \Resource
- class \ResourceUpload
Expanded class hierarchy of ResourceUpload
File
- resource/
resource.module, line 331 - Resource API for Drupal, a replacement for files.
View source
class ResourceUpload extends Resource {
public $source = '';
public $errors;
function save() {
// validate if not already validated, error is validation didn't pass.
if (!$this
->validate()) {
return FALSE;
}
// rename the file to its original name.
parent::save();
}
function validate($validators = array()) {
if (!isset($this->errors)) {
// Default validation for all uploads.
$validators['file_validate_name_length'] = array();
$this->errors = array();
foreach ($validators as $function => $args) {
array_unshift($args, $file);
$errors = array_merge($errors, call_user_func_array($function, $args));
}
}
return !empty($this->errors);
}
function errors() {
return $this->errors;
}
function move($dest) {
// validate if not already validated, error is validation didn't pass.
if (!$this->fid && !$this
->save()) {
return FALSE;
}
// for upload files we changed the filename in the path to a junk string...
}
/**
* Saves a file upload to a temporary location
*
* The file will be added to the files table as a temporary file. Temporary
* files are periodically cleaned. To make the file permanent file call
* it's set_permanent() method.
*
* @param $source
* A string specifying the name of the upload field to save.
* @param $validators
* An optional, associative array of callback functions used to validate the
* file. The keys are function names and the values arrays of callback
* parameters which will be passed in after the user and file objects. The
* functions should return an array of error messages, an empty array
* indicates that the file passed validation. The functions will be called in
* the order specified.
* @param $destination
* A string containing the directory $source should be copied to. If this is
* not provided or is not writable, the temporary directory will be used.
* @param $replace
* A boolean indicating whether an existing file of the same name in the
* destination directory should overwritten. A FALSE value will generate a
* new, unique filename in the destination directory.
* @return
* An object containing the file information, or FALSE in the event of an
* error.
*/
static function save_upload($source) {
// check and see if there were any errors.
if (drupal_file_upload::upload_error($source)) {
return FALSE;
}
// Begin building file object.
$file = new stdClass();
$file->source = $source;
$file->uid = $user->uid;
$file->filename = basename($_FILES['files']['name'][$source]);
// create a tmp path to use until the file is save to a final location else where.
// we just use a random string to defang the file for processing in tmp.
$file->filepath = file_destination(uniqid(), file_directory_temp(), FALSE);
$file->filemime = $_FILES['files']['type'][$source];
$file->filesize = $_FILES['files']['size'][$source];
// Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary
// directory. This overcomes open_basedir restrictions for future file
// operations.
if (!move_uploaded_file($_FILES['files']['tmp_name'][$source], $file->filepath)) {
form_set_error($source, t('File upload error. Could not move uploaded file.'));
watchdog('file api', 'Upload error. Could not move uploaded file %file to destination %destination.', array(
'%file' => $file->filename,
'%destination' => $file->filepath,
));
return FALSE;
}
return $file;
}
function upload_error($errno) {
// If no file was uploaded there is an error. :)
if (empty($_FILES['files']['tmp_name'][$source]) || !is_uploaded_file($_FILES['files']['tmp_name'][$source])) {
return t('No file uploaded');
}
// @see http://php.net/manual/en/features.file-upload.errors.php
switch ($_FILES['files']['error'][$source]) {
case UPLOAD_ERR_OK:
return FALSE;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
return t('The file %file could not be saved, because it exceeds %maxsize, the maximum allowed size for uploads.', array(
'%file' => $source,
'%maxsize' => format_size(file_upload_max_size()),
));
case UPLOAD_ERR_PARTIAL:
case UPLOAD_ERR_NO_FILE:
return t('The file %file could not be saved, because the upload did not complete.', array(
'%file' => $source,
));
case UPLOAD_ERR_NO_TMP_DIR:
return t('The file %file could not be saved, because the PHP upload_tmp_dir does not exist.', array(
'%file' => $source,
));
case UPLOAD_ERR_CANT_WRITE:
return t('The file %file could not be saved, because the file could not be written to the disk.', array(
'%file' => $source,
));
case UPLOAD_ERR_EXTENSION:
return t('The file %file could not be saved, because the upload was stopped by a php extension.', array(
'%file' => $source,
));
// Unknown error
default:
return t('The file %file could not be saved. An unknown error has occurred.', array(
'%file' => $source,
));
}
}
// Use static load as the entry point to keep the API interface
// consistent.
function load($source) {
if (isset(self::$files[$source])) {
return self::$files[$source];
}
// attempt to save the upload.
if ($file = self::save_upload($source)) {
return new ResourcePublic($file);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Resource:: |
protected static | property | ||
Resource:: |
protected | property | ||
Resource:: |
protected | property | ||
Resource:: |
protected | property | ||
Resource:: |
protected | property | ||
Resource:: |
protected | property | ||
Resource:: |
protected | property | ||
Resource:: |
protected static | property | ||
Resource:: |
protected static | property | ||
Resource:: |
protected | property | ||
Resource:: |
protected | property | ||
Resource:: |
protected | property | ||
Resource:: |
function | Create a copy a Resource. | ||
Resource:: |
function | Delete a file and its database record. | ||
Resource:: |
function | |||
Resource:: |
function | Mark a file as permanent. | ||
Resource:: |
function | Create a new name for a resource by appending a number if a resource exists at $destination. | ||
Resource:: |
protected | function | drupal_file constructor. | |
ResourceUpload:: |
public | property | ||
ResourceUpload:: |
public | property | ||
ResourceUpload:: |
function | |||
ResourceUpload:: |
function | |||
ResourceUpload:: |
function |
Move a Resource. Overrides Resource:: |
||
ResourceUpload:: |
function |
Save the current state of a drupal_file in the database.
If the file->fid is empty a new database record will be added. Overrides Resource:: |
||
ResourceUpload:: |
static | function | Saves a file upload to a temporary location | |
ResourceUpload:: |
function | |||
ResourceUpload:: |
function |