/    Sign up×
Community /Pin to ProfileBookmark

How to modify upload script to accept file type

How do I change this:

[CODE]<?php
$target_path = “uploads/”;

$target_path = $target_path . basename( $_FILES[‘uploadedfile’][‘name’]);

if(move_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’], $target_path)) {
echo “The file “. basename( $_FILES[‘uploadedfile’][‘name’]).
” has been uploaded”;
} else{
echo “There was an error uploading the file, please try again!”;
}
?>[/CODE]

so that I allow only .doc files and files smaller than 20KB to be uploaded?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@martswiteMar 27.2009 — Try this.

[code=php]<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

//Put the posted file type into a var for easier reference.
$fPosted = $_FILES['uploadedfile']['type'];

//Array that contains all the allowed file types
$fAllowed = array("application/msword");

//Check to see if the posted file is in the allowed files array.
if(!in_array($fPosted, $fAllowed)){
echo "Incorrect file type only .doc files please";
}else{
if ($_FILES["file"]["size"] > 2560){
echo "Error: Your file is to big only files less than 20kb please<br />";
}else{

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>[/code]


This should do what you described. I may be wrong but the application/msword may only accept documents that were originated in ms word. You can however add more types to that $fAllowed array, so you might be more comfortable having something like this.

[code=php]
$fAllowed = array("application/msword", "application/doc", "application/word");
[/code]


There are a few more mime types for word documents and the like. I found them at this website.

.doc mime types

Hope this helps.
×

Success!

Help @Limozine spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...