Related topics
- File interface
- Common file handling functions.
File
- includes/file.inc, line 170
- API for handling file uploads and server file management.
Code
function file_check_upload($source = 'upload') {
static $upload_cache;
if (is_object($source)) {
if (is_file($source->filepath)) {
return $source;
}
else {
return false;
}
}
if (isset($upload_cache[$source])) {
return $upload_cache[$source];
}
if ($_FILES["edit"]["name"][$source] && is_uploaded_file($_FILES["edit"]["tmp_name"][$source])) {
switch ($_FILES["edit"]["error"][$source]) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
drupal_set_message(t('The file %file could not be saved, because it exceeds the maximum allowed size for uploads.', array('%file' => theme('placeholder', $source))), 'error');
return 0;
case UPLOAD_ERR_PARTIAL:
case UPLOAD_ERR_NO_FILE:
drupal_set_message(t('The file %file could not be saved, because the upload did not complete.', array('%file' => theme('placeholder', $source))), 'error');
return 0;
default:
drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', array('%file' => theme('placeholder', $source))), 'error');
return 0;
}
$file = new StdClass();
$file->filename = trim(basename($_FILES["edit"]["name"][$source]), '.');
$file->filepath = tempnam(file_directory_temp(), 'tmp_');
$file->filemime = $_FILES["edit"]["type"][$source];
if (((substr($file->filemime, 0, 5) == 'text/' || strpos($file->filemime, 'javascript')) && (substr($file->filename, -4) != '.txt')) || preg_match('/\.(php|pl|py|cgi|asp)$/i', $file->filename)) {
$file->filemime = 'text/plain';
$file->filepath .= '.txt';
$file->filename .= '.txt';
}
if (!move_uploaded_file($_FILES["edit"]["tmp_name"][$source], $file->filepath)) {
drupal_set_message(t('File upload error. Could not move uploaded file.'));
watchdog('file', t('Upload Error. Could not move uploaded file(%file) to destination(%destination).', array('%file' => theme('placeholder', $_FILES["edit"]["tmp_name"][$source]), '%destination' => theme('placeholder', $file->filepath))));
return false;
}
$file->filesize = $_FILES["edit"]["size"][$source];
$file->source = $source;
$upload_cache[$source] = $file;
return $file;
}
else {
if (file_exists($_SESSION['file_uploads'][$source]->filepath)) {
return $_SESSION['file_uploads'][$source];
}
}
return false;
}
Login or
register to post comments