Sunday, September 30, 2012

PHP สคริปอับโหดและพรีวิวภาพ

แบ่งออกเป็น 3 ไฟล์นะครับ

ไฟล์ที่ 1 เอาไว้โชว์ภาพ ในที่นี้ผมตั้งเป็น index.html
Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function popupUploadImage( f_name, e_name, path ) {
window.open("./popUploadImages.php","name","top=300,left=450,height=250,width=400, status=1");
}

</script> </head>
<body>
<img src="" name="pImage" id="pImage"/>
<br />
<a href="#" onclick="popupUploadImage();"><b>[ Browse ]</b></a><br />
</body>
</html>


ไฟล์ที่ 2 popUploadImages.php
Code
<html>
<head>
<title>Upload Images</title>
</head>

<body>
<form name="uploadImage" action="uploadImages.php" method="post" enctype="multipart/form-data">
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="darkbrown12_bold">?</td>
</tr>
<tr>
<td class="darkbrown12_bold"><div align="center">Select Image</div></td>
</tr>
<tr height="35">
<td align="center"><input type="file" name="file" class="inputBox" size="30"></td>
</tr>
<tr height="35">
<td align="center">?</td>
</tr>
<tr height="35">
<td align="center">
<input type="submit" name="submitPage" value="upload" class="button80">
</td>
</tr>
</table>
</form>
</body>
</html>


ไฟล์ที่ 3 uploadImages.php
Code
<?php

if($file){
$path = "images/";
$fileType = explode('.', $file_name);
$fileName = date('YmdHis');
$fileName = $fileName.".".$fileType[1];
$uploadFile = $path."".$fileName;
copy($file, $uploadFile);


$img = "./images/$fileName";
$Ipreview = "pImage";


}

?>

<html>
<head>

<script language="javascript">
function returnValue() {
window.opener.document.getElementById('<? echo $Ipreview?>').src = "<?php echo $img ?>";
window.close();
}
</script>
</head>
<body onLoad="returnValue()">
</body>
</html>

No comments:

Post a Comment