-
September 21st, 2003, 03:06 AM
#1
check file type in php
I have this and it works fine for uploading any file
<?php
// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of
// $_FILES. In PHP earlier then 4.0.3, use copy() and is_uploaded_file()
// instead of move_uploaded_file
$uploaddir = '/usr/local/apache2/htdocs/uploads/';
$uploadfile = $uploaddir. $_FILES['userfile']['name'];
print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
print "File is valid, and was successfully uploaded. ";
} else {
print "upload failed. ";
}
print "</pre>";
?>
But I want to put a restriction to where only image files can be uploaded ( jpg, gif, png).
How do I do this?
Thanks.
EDIT
I think I got it to work, does this look right...
<?php
// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of
// $_FILES. In PHP earlier then 4.0.3, use copy() and is_uploaded_file()
// instead of move_uploaded_file
$uploaddir = '/usr/local/apache2/htdocs/uploads/';
$imgtype = $_FILES['userfile']['type'];
$uploadfile = $uploaddir. $_FILES['userfile']['name'];
print "<pre>";
if($imgtype == "image/x-png" || $imgtype == "image/pjpeg" || $imgtype == "image/gif"){
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
print "File is valid, and was successfully uploaded. ";
} else {
print "upload failed. ";
}
}
else {
print "upload failed. ";
}
print "</pre>";
?>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|