Powered By Blogger

Saturday, February 15, 2014

upload image and store in database

<form action="" method="post" enctype="multipart/form-data">
<table width="500" border="0" cellspacing="10">
  <tr>
    <td>Upload Image</td>
    <td><input type="file" name="file" id="file"></td>
  </tr>
  <tr>
    <td>Youtube Link</td>
    <td><input type="text" name="y_link" /></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="submit"></td>
  </tr>
</table>
</form>

<?php
if(isset($_POST['y_link'])){
$y_link = $_POST['y_link'];

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& 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>";

    if (file_exists("images/upload/youtube/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "images/upload/youtube/" . $_FILES["file"]["name"]);
      echo  "images/upload/youtube/" . $_FILES["file"]["name"];
 mysql_query("INSERT INTO `youtube`(`image`, `link`) VALUES ('".$img."','".$y_link."')");
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}
?>

No comments:

Post a Comment