Uploaded Word, Excel files become gibberish

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
druffus
Forum Newbie
Posts: 1
Joined: Tue Oct 07, 2003 6:14 am

Uploaded Word, Excel files become gibberish

Post by druffus »

Hello all,

I have some code to upload files through a form. Well, if I download the file through the web or with ftp, it is all gibberish that Word can't read. So, getting the file there works fine, but it seems that it is stripping some formatting or something else. Here is the code that I use to upload:

Code: Select all

if(!empty($HTTP_POST_FILES['uploadedfile'])){
		$HTTP_POST_FILES['uploadedfile']['name'] = strip_tags ($HTTP_POST_FILES['uploadedfile']['name']);
        $HTTP_POST_FILES['uploadedfile']['name'] = str_replace($snr,"",$HTTP_POST_FILES['uploadedfile']['name']);  // remove any % signs from the file name
        $HTTP_POST_FILES['uploadedfile']['name'] = trim($HTTP_POST_FILES['uploadedfile']['name']);
		
		if($HTTP_POST_FILES['uploadedfile']['size']>0 && $HTTP_POST_FILES['uploadedfile']['size']<$MaxFileSize) {
			if( !file_exists($config['uploaded_files_path']) ){
				$oldumask = umask(0); 
  				$result = mkdir($config['uploaded_files_path'], 01777); // 01777 so you get the sticky bit set 
  				umask($oldumask);
				if(!$result) {
               		$error['file']['message'] = "Could not be created.  Contact Administrator.";
					$error['file']['class'] = "form_error";
            	}
			}
			$chmod_result = chmod($config['uploaded_files_path'], 01777);
			if( !$chmod_result ){
				$error['file']['message'] = "Could not open with correct permissions.";
				$error['file']['class'] = "form_error";
			}
			$upload_result = move_uploaded_file($HTTP_POST_FILES['uploadedfile']['tmp_name'], $config['uploaded_files_path'].$HTTP_POST_FILES['uploadedfile']['name']);
			if( !$chmod_result ){
				$error['file']['message'] = "Could Not upload file.";
				$error['file']['class'] = "form_error";
			}
			// For some reason this screws everything up, but i am trying to set limited permissions on the folder
			//$chmod_result = chmod($config['uploaded_images_path'], 0644);
			//if( !$chmod_result ){
			//	$error['file']['message'] = "Permissions were not closed";
			//	$error['file']['class'] = "form_error";
			//}
			
			$path=$config['uploaded_files_path'].$HTTP_POST_FILES['uploadedfile']['name'];
		}else{
			$error['file']['message'] = "File too large.  Contact Administrator";
			$error['file']['class'] = "form_error";
		}
	}else{
		$error['file']['message'] = "No Image File - Please try again.";
		$error['file']['class'] = "form_error";
	}

Other files like images seem to work fine. Any ideas would help. Thanks!
Post Reply