# FILE UPLOAD

(1)step 1: write code for “slider.html”

<form action=”sliderupload.php” method=”post” enctype=”multipart/form-data”>
UPload<input type=”file” name=”image”>
<input type=”submit” name=”submit” value=”Upload”>

</form>

(2)step 2: write code for “sliderupload.html”

<?php

$image=$_FILES[‘image’][‘name’];

include(“connect.php”);

$sql=”insert into slider(image)values(‘$image’)”;

mysql_query($sql) or die(mysql_errror());

move_uploaded_file($_FILES[‘image’][‘tmp_name’],”slider/”.$_FILES[‘image’][‘name’]) or die(mysql_error());

?>

(3)write code for class.php file :

<?php

class db

{

var $host=”servername”;

var $username=”username”;

var $password=”password”;

function connect()
{

$connect=mysql_connect($this->host,$this->username,$this->password);

mysql_select_db(“a1390987_om”,$connect) or die(mysql_error());

if(!$connect)
{

echo “connection error “;
}

}

function display($table)
{

$sql=”select * from $table “;
$result=mysql_query($sql);
$row=mysql_fetch_array($result);

return $row;

}

}

$obj=new db();
$obj->connect();

?>

(4)use img tag to display image & write following code for displayslider.php:

<?php

include(“class.php”);

$table=”slider”;

$slider=$obj->display($table);

$image=$slider[‘image’];

echo $image;

echo “<img src=’slider/”.$image.”‘ height=200 width=200 border=10 >”;

?>
========================================================================
write above code with restriction of image uploading
========================================================================
(1)write code for uploadform.html:

<form id=”uploadimage” action=”upload.php” method=”post” enctype=”multipart/form-data”>

<hr id=”line”>

Select Your Image

</form>

(2)upload.php:

<?php
if(isset($_FILES[“file”][“type”]))
{
$validextensions = array(“jpeg”, “jpg”, “png”);
$temporary = explode(“.”, $_FILES[“file”][“name”]);
$file_extension = end($temporary);
if ((($_FILES[“file”][“type”] == “image/png”) || ($_FILES[“file”][“type”] == “image/jpg”) || ($_FILES[“file”][“type”] == “image/jpeg”)
) && ($_FILES[“file”][“size”] < 400000)//Approx. 400kb files can be uploaded.
&& in_array($file_extension, $validextensions))
{
if ($_FILES[“file”][“error”] > 0)
{
echo “Return Code: ” . $_FILES[“file”][“error”] . “<br/><br/>”;
}
else
{
if (file_exists(“upload/” . $_FILES[“file”][“name”])) {
echo $_FILES[“file”][“name”] . ” <span id=’invalid’><b>already exists.</b></span> “;
}
else
{
$sourcePath = $_FILES[‘file’][‘tmp_name’]; // Storing source path of the file in a variable
$targetPath = “upload/”.$_FILES[‘file’][‘name’]; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
echo “<span id=’success’>Image Uploaded Successfully…!!</span><br/>”;
echo “<br/><b>File Name:</b> ” . $_FILES[“file”][“name”] . “<br>”;
echo “<b>Type:</b> ” . $_FILES[“file”][“type”] . “<br>”;
echo “<b>Size:</b> ” . ($_FILES[“file”][“size”] / 1024) . ” kB<br>”;
echo “<b>Temp file:</b> ” . $_FILES[“file”][“tmp_name”] . “<br>”;
}
}
}
else
{
echo “<span id=’invalid’>***Invalid file Size or Type***<span>”;
}
}
?>