Wednesday, 27 December 2017

AWT-Slip15

1) Write a PHP Script to Upload the file and display its information.(use $_FILES).      

slip15.html
<html>
<body>

<form action="upload.php" method="post"enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>

</html>

slip15.php
<html>
<BODY>
<form action="upload.php" method="post"  enctype="multipart/form-data">

<B>File:</b><input type="file" name="file" id="file" />

<input type=submit name="submit" value="submit"/>
</form>
</body>
</html>


upload.php
<?php
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
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>";
// Enter your path to upload file here
if (file_exists("c:\wamp\www\upload/newupload/" .
$_FILES["file"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
" already exists. "."</div>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"c:\wamp\www\upload/newupload/" . $_FILES["file"]["name"]);
echo "<div class='sucess'>"."Stored in: " .
"c:\wamp\www\upload/newupload/" . $_FILES["file"]["name"]."</div>";
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
?>

Another Solution

<html>
<body>
<form method=post action="Uploadfile.php" enctype="multipart/form-data">
File Name : <input type="file" name="file" id="file" size=40%><br>
<input type=submit name=submit value=submit>
</form>
</body>
</html>

Uploadfile.PHP

<?php
if($_FILES["file"]["error"]>0)
{
echo"Error : ".$_FILES["file"]["error"]."<br>";
}
else
{
echo"Upload File : ".$_FILES["file"]["name"]."<br>";
echo"Type : ".$_FILES["file"]["type"]."<br>";
echo"Size : ".($_FILES["file"]["size"]/1024)." kb<br>";
echo"Temporary Storage : ".$_FILES["file"]["tmp_name"];
}

?>           

                                                                                                                                                              
2)Write Ajax program to fetch suggestions when isuser is typing in a textbox. (eg like Google suggestions. Hint create array of suggestionsand matching string will be displayed)  



fetch.html

<html>
<head>
<script>
function showMatch(str)
{            
if (str.length==0)
{
document.getElementById("mydiv").innerHTML = "";
return;
}
if(window.XMLHttpRequest)
{
XHRobj=new XMLHttpRequest();
}
else
{
XHRobj=new ActiveXObject("Microsoft.XMLHTTP");
}
XHRobj.onreadystatechange = function()
{
if (XHRobj.readyState == 4 && XHRobj.status == 200)
{
document.getElementById("mydiv").innerHTML = XHRobj.responseText;
}
}
XHRobj.open("GET", "fetch.php?q=" +str, true);
XHRobj.send();
}
</script>
</head>
<body>
<p><b>Suggestion:</b></p>
<form>
Search<input type="text" onkeyup="showMatch(this.value)">
</form>
<div id="mydiv"><b> info......</b></div>
</body>
</html>

fetch.php
<?php
$a=array("apple","litchi","banna","anar");
$q=$_GET["q"];
if(strlen($q)>0)
{
$match="";
for($i=0;$i<count($a);$i++)
{
if(strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if($match=="")
{
$match=$a[$i];
}
else
{
$match=$match.",".$a[$i];
}
}
}
}
if($match=="")
{
echo "no suggestion";
}
else
{
echo $match;
}
?>