![]() |
Home My Faqts Contributors About Help |
| Entry | Add Entry Alert - Edit this Entry |
Sep 4th, 2001 03:55
PHP has the posibility to recognize the MIME type of the uploaded file.
For your case you need such MIME types
like 'image/gif', 'image/jpeg', 'image/png', 'image/tiff' etc. , so
ones knowing the MIME type you check if it begins with the
string 'image'. A simple way of doing that is:
///file upload.php - > form with upload input tag/////
///ENCTYPE="multipart/form-data" -this is neccessary!
<html>
<body>
<form method="post" name="form1" action="check.php"
ENCTYPE="multipart/form-data">
<INPUT name="file1" type="file" onchange="document.form1.submit();">
</form>
</body>
</html>
///file check.php if image save it to the server
///main directory and set it as a background.
<?php
$type=$file1_type; //gets the MIME type
if(ereg ("^image", $type)) {$g=0;}
else $g=1;
if (!$g) {
$img_s="/" . $file1_name;
if (file_exists($img_s))
unlink($img_s);
copy($file1,$img_s);
}
?>
<html>
<body <?php if(isset($img_s)) printf(" background=\"%s\" ",$img_s); ?> >
<SCRIPT LANGUAGE="JavaScript">
<?php
if ($g) {
printf("alert('This is not a valid image type!');\nwindow.close
();\n");
}
?>
</SCRIPT>
</body>
</html>
© 1999-2004 Synop Pty Ltd