Lassie Posted July 21, 2009 Share Posted July 21, 2009 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();?> Quote Link to comment https://forums.phpfreaks.com/topic/166780-solved-cant-upload-doc-type/ Share on other sites More sharing options...
ignace Posted July 21, 2009 Share Posted July 21, 2009 image/doc is not the mimetype of a word document it's application/msword Quote Link to comment https://forums.phpfreaks.com/topic/166780-solved-cant-upload-doc-type/#findComment-879502 Share on other sites More sharing options...
Lassie Posted July 21, 2009 Author Share Posted July 21, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/166780-solved-cant-upload-doc-type/#findComment-879564 Share on other sites More sharing options...
Alex Posted July 21, 2009 Share Posted July 21, 2009 // create an array of permitted MIME types $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png','application/msword'); Quote Link to comment https://forums.phpfreaks.com/topic/166780-solved-cant-upload-doc-type/#findComment-879579 Share on other sites More sharing options...
Lassie Posted July 22, 2009 Author Share Posted July 22, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/166780-solved-cant-upload-doc-type/#findComment-880176 Share on other sites More sharing options...
ignace Posted July 22, 2009 Share Posted July 22, 2009 // 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'); Quote Link to comment https://forums.phpfreaks.com/topic/166780-solved-cant-upload-doc-type/#findComment-880316 Share on other sites More sharing options...
Lassie Posted July 22, 2009 Author Share Posted July 22, 2009 Ok. I can get some .doc to upload. I will refine the array. Thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/166780-solved-cant-upload-doc-type/#findComment-880419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.