Monday, April 15, 2013

Image upload using php without page referesh

<!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=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function confirmFun(me)
{
if(confirm("Are You Want To Delete"))
{

document.frm.action ="image_upload.php?act=DEL";
document.frm.target = "imgFrame";
document.frm.submit();
return true;
}
else
{
me.checked = false;
return false;
}

}
function uploadImg()
{
document.frm.action ="image_upload.php?act=UP";
document.frm.target = "imgFrame";
document.frm.submit();

}
</script>
</head>

<body>
<form name="frm" action="<?=$_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
<p>File1:
<input type="file" name="img" id="img" value="" onchange="return uploadImg()" />
Please Upload JPG,GIFor BMP.</p>

<p><br />
<img id="imgId" border="0" style="display:none" width="100" height="100"/><br />
<div id="del" style="display:none">
<input type="radio" id="radioBtn" value="D" onclick="return confirmFun(this);">Delete Image
</div>
<input type="hidden" name="hdnImgNm" id="hdnImgNm" value = "" />
</p>
<iframe name="imgFrame" style="display:none"></iframe>
</form>
</body>
</html>




<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

No comments:

Post a Comment

Thank you for your Comment....

Popular Posts