Browse Source

manual merge which may have broken something about MoveUploadedFile()

Yannick Warnier 11 years ago
parent
commit
e136bda0d1
3 changed files with 14 additions and 10 deletions
  1. 1 2
      main/document/upload.php
  2. 1 1
      main/inc/ajax/document.ajax.php
  3. 12 7
      main/inc/lib/fileUpload.lib.php

+ 1 - 2
main/document/upload.php

@@ -258,8 +258,7 @@ if (api_get_setting('search_enabled') == 'true') {
 
 $form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
 $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
-$form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');
-
+$form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename', array('checked="checked"'));
 // Close the java script and avoid the footer up
 $form->addElement('html', '</div>');
 

+ 1 - 1
main/inc/ajax/document.ajax.php

@@ -33,7 +33,7 @@ switch($action) {
                 $file['name'],
                 null,
                 0,
-                'overwrite',
+                $_POST['if_exists'],
                 false,
                 false
             );

+ 12 - 7
main/inc/lib/fileUpload.lib.php

@@ -257,17 +257,18 @@ function handle_uploaded_document(
 			$file_size = $uploaded_file['size'];
 
 			$files_perm = api_get_permissions_for_new_files();
-
-			// What to do if the target file exists
+                       $doc_path = '/'.$clean_name;
+                       $docId = DocumentManager :: get_document_id($_course, $doc_path, $current_session_id);
+      		        // What to do if the target file exists
 			switch ($what_if_file_exists) {
 				// Overwrite the file if it exists
 				case 'overwrite':
-					// Check if the target file exists, so we can give another message
+                                       // Check if the target file exists, so we can give another message
 					$file_exists = file_exists($store_path);
 					if (moveUploadedFile($uploaded_file, $store_path)) {
 						chmod($store_path, $files_perm);
 
-						if ($file_exists) {
+						if ($file_exists && $docId) {
 							// UPDATE DATABASE
 							$document_id = DocumentManager::get_document_id($_course, $file_path);
 
@@ -327,7 +328,11 @@ function handle_uploaded_document(
 
 				// Rename the file if it exists
 				case 'rename':
+                                  if ($docId) {
 					$new_name = unique_name($where_to_save, $clean_name);
+                                  } else {
+                                      $new_name = $clean_name;
+                                  }
 					$store_path = $where_to_save.$new_name;
 					$new_file_path = $upload_path.$new_name;
 
@@ -361,13 +366,13 @@ function handle_uploaded_document(
 					break;
 				default:
                     // Only save the file if it doesn't exist or warn user if it does exist
-					if (file_exists($store_path)) {
+					if (file_exists($store_path) && $docId) {
 					    if ($output) {
 						  Display::display_error_message($clean_name.' '.get_lang('UplAlreadyExists'));
 						}
 					} else {
-						if (moveUploadedFile($uploaded_file, $store_path)) {
-							chmod($store_path, $files_perm);
+						if (@move_uploaded_file($uploaded_file['tmp_name'], $store_path)) {
+						    chmod($store_path, $files_perm);
 
 							// Put the document data in the database
 							$document_id = add_document($_course, $file_path, 'file', $file_size, $document_name, null, 0, true);