faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 14 people (64%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How to do attachment if I want to use a file as background?

Feb 22nd, 2008 20:17
dman, Dimitur Kirov, Selina Chong, http://www.ttnr.org


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>