Jump to content

[SOLVED] Cant upload doc type


Lassie

Recommended Posts

I have a simple upload program designed to upload images that I want to be able to alos upload word docs.

I have amended my permitted array to include doc type but the upload is refused.

Should i be using something else in my permitted array?

<?php
/*
Template Name: registration
*/
?>
<?php get_header();?>
<div id="leftnav">

<?php include (TEMPLATEPATH . '/sidebar1.php'); ?>

<div id="right_pan".
</div>
</div>

<div id="rightnav">
</div>

<div id="content">
<p>Upload Book details - Simple version</p>
<?php

get_currentuserinfo();
global $current_user;
if (isset($current_user)){
  echo $current_user->user_login;
}


// define a constant for the maximum upload size
		define ('MAX_FILE_SIZE', 51200 );
  		if (array_key_exists('upload', $_POST)) 
  		print_r($_FILES);
	{
  		// define constant for upload folder
  		define('UPLOAD_DIR', 'C:/upload_test/');
  		// replace any spaces in original filename with underscores
  		// at the same time, assign to a simpler variable
  		$file = str_replace(' ', '_', $_FILES['image']['name']);
  		
  		// convert the maximum size to KB
  			$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
  			echo $max;
  			// create an array of permitted MIME types
  			$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png','image/doc');
  			// begin by assuming the file is unacceptable
  			$sizeOK = false;
  			$typeOK = false;
  		
  
  		// check that file is within the permitted size
  			if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) 
		{
    			$sizeOK = true;
		}

		// check that file is of an permitted MIME type
  				foreach ($permitted as $type) 
			  {
    			if ($type == $_FILES['image']['type']) 
			{
      				
				  $typeOK = true;
  				break;
  			}
		}
  			if ($sizeOK && $typeOK) 
		  {
    			switch($_FILES['image']['error']) 
			{
  			case 0:
  			//take user name or id from session variable
  			//$username = $_SESSION['first_name'];
  			
			// if the user's subfolder doesn't exist yet, create it
			if (!is_dir(UPLOAD_DIR.$current_user->user_login)) 
			{
	  			mkdir(UPLOAD_DIR.$current_user->user_login);
	  		}
        		// check if a file of the same name has been uploaded
  		// check if a file of the same name has been uploaded. if it has timestamp duplicate
			if (!file_exists(UPLOAD_DIR.$current_user->user_login.'/'.$file)) 
			{
       		 // move the file to the upload folder and rename it
			$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$current_user->user_login.'/'.$file);
			}
			//timestamp duplicate
			else{

			$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$current_user->user_login.'/'.time().$file);
			}
			if ($success) {
          		$result = "$file uploaded successfully";
      		}
			else {
	  		$result = "There was an error uploading $file. Please try again.";
	  		}
    		break;
  			case 3:
			$result = "There was an error uploading $file. Please try again.";
  			default:
        		$result = "System error uploading $file. Contact webmaster.";
  			}
    		}
  				elseif ($_FILES['image']['error'] == 4) {
    					$result = 'No file selected';
			}
  else {
    $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png, doc.";
}
  }
  
	// if the form has been submitted, display result
		if (isset($result)) 
		{
  			echo "<p>$result</p>";
  			}
?>

  			<h2>Upload Your Book File</h2>
  			
    <div style="text-align: center">
    <table width = \"390\" border = 0 align="left">
    <tr><td>
	  <form action="" method="post" enctype="multipart/form-data" name="uploadImage" id="uploadImage"> 
	  <fieldset>
  			<legend>Upload Your Book Synopsis</legend>
    	<p>
	<label for="image">Upload image:<em class="required">(only jpg,gif,png,doc.Max size 50kb)</em></label>
	<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
        <input type="file" name="image" id="image" />
    	</p>
    	<p>
        <input type="submit" name="upload" id="upload" value="Upload" />
    	</p></fieldset>	
	</form>
	</td></tr></table>

</div> 	
<?php get_footer();?>

Link to comment
Share on other sites

Thanks.

I modfied the code as follows but still fails condition

Any thoughts appreciated.

// create an array of permitted MIME types
  			$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png','application/msword/doc');

// check that file is of an permitted MIME type
  				foreach ($permitted as $type) 
			  {
    			if ($type == $_FILES['image']['type']||$type == $_FILES['application/msword']['type']) 
			{
      				
				  $typeOK = true;
  				break;

Link to comment
Share on other sites

Thanks. Still no joy.

print_r ($_FILES) showsthe following

 

Array ( [image] => Array ( [name] => Kevin_Read_Me.doc [type] => application/vnd.ms-word [tmp_name] => C:\PROGRA~1\EASYPH~1\\tmp\php4D.tmp [error] => 0 => 19456 ) ) 50.0KB

Link to comment
Share on other sites

// create an array of permitted MIME types
  			$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png','application/msword');

 

 

Just extend what alex wrote:

 

// create an array of permitted MIME types
  			$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png','application/msword', 'application/vnd.ms-word');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.