function webfm_version_upload in Web File Manager 5
Version uploaded file that overwrites existing file NOTE: uploaded file is already stored in $_SESSION['temp_upload'] via iframe. Feedback in ajax since drupal_set_message points back into main page (not refreshed).
2 calls to webfm_version_upload()
- webfm_ajax in ./
webfm.module - Ajax post requests
- webfm_upload in ./
webfm.module - Called by upload form submit
File
- ./
webfm.module, line 1640
Code
function webfm_version_upload($op, &$msg) {
global $user;
$ret = TRUE;
switch ($op) {
case WEBFM_CANCEL:
//Cancel button
$ret = FALSE;
$msg = t('Upload Cancelled');
break;
case WEBFM_RENAME_NEW:
$ret = webfm_upload_new($msg);
break;
case WEBFM_REPLACE_RENAME:
// Retrieve file record for original file
// Rename (munge) original file
// Move new file to destination
// Update existing original record with data from new file
// Create new db record for original file
// Restore original file if error
$path = $_SESSION['temp_upload']->dest . '/' . $_SESSION['temp_upload']->filename;
if ($pos = strrpos($path, '.')) {
$name = substr($path, 0, $pos);
$ext = substr($path, $pos);
}
else {
$name = $basename;
}
$counter = 0;
do {
$temp_path = $name . '_' . $counter++ . $ext;
} while (file_exists($temp_path));
@rename($path, $temp_path);
if (file_move($_SESSION['temp_upload'], $_SESSION['temp_upload']->dest)) {
// file was moved to its final destination
if ($_SESSION['temp_upload']->in_db == TRUE) {
if (($record = webfm_get_file_record('', $path)) === FALSE) {
$ret = FALSE;
$msg = t('Replace-Delete webfm_get_file_record fail');
break;
}
//Insert file into database if under webfm_root
$time = @filemtime($_SESSION['temp_upload']->filepath);
$query = "UPDATE {webfm_file} SET fsize = %d, fcreatedate = %d, fversion = %d, uid = %d WHERE fid = %d";
$row = db_query($query, $_SESSION['temp_upload']->filesize, $time, $record->fversion + 1, $user->uid, $record->fid);
if ($row === FALSE) {
// Update of existing record failed
// Delete new file and restore name of original file
$ret = FALSE;
$msg = t('Replace-Delete update fail');
file_delete($path);
@rename($temp_path, $path);
}
else {
// Create new record for existing file using old record
$newfile = new stdClass();
$newfile->filepath = $temp_path;
$ret = webfm_dbinsert_file($newfile, $msg, (array) $record);
if ($ret !== FALSE) {
$msg = t('Upload success');
}
}
}
else {
// No ajax returned since called by upload directly without version options
$ret = webfm_upload_new($msg);
}
}
else {
$ret = FALSE;
$msg = t('file_move fail');
@rename($temp_path, $path);
}
break;
case WEBFM_REPLACE_DELETE:
// Retrieve db record for original file & rename file
// Move new file to destination
// Update existing original record with data from new file
// Restore original file if error, else delete
$path = $_SESSION['temp_upload']->dest . '/' . $_SESSION['temp_upload']->filename;
//use temp for rollback on error
$temp_path = $path . '~';
@rename($path, $temp_path);
if (file_move($_SESSION['temp_upload'], $_SESSION['temp_upload']->dest)) {
// file was moved to its final destination
if ($_SESSION['temp_upload']->in_db == TRUE) {
if (($record = webfm_get_file_record('', $path)) === FALSE) {
$ret = FALSE;
$msg = t('Replace-Delete webfm_get_file_record fail');
break;
}
//Insert file into database if under webfm_root
$time = @filemtime($_SESSION['temp_upload']->filepath);
$query = "UPDATE {webfm_file} SET fsize = %d, fcreatedate = %d, fversion = %d, uid = %d WHERE fid = %d";
$row = db_query($query, $_SESSION['temp_upload']->filesize, $time, $record->fversion + 1, $user->uid, $record->fid);
if ($row === FALSE) {
$ret = FALSE;
$msg = t('Replace-Delete update fail');
// Delete new file and restore name of original file
file_delete($path);
@rename($temp_path, $path);
}
else {
$ret = $record->fid;
$msg = t('Upload success');
file_delete($temp_path);
}
}
else {
file_delete($temp_path);
$ret = TRUE;
$msg = t('Upload success');
}
}
else {
$ret = FALSE;
$msg = t('file_move fail');
@rename($temp_path, $path);
}
break;
default:
$ret = FALSE;
$msg = '';
break;
}
return $ret;
}