You are on page 1of 11

1) How to Combining HTML and PHP code on a single page?

<?php
$num_to_guess = 42;
$message = "";
if (!isset($_POST['guess']))
{
$message = "Welcome to the guessing machine!";
}
elseif(!is_numeric($_POST['guess']))
{
$message ="i dont understand that respone";
}

elseif ($_POST['guess'] > $num_to_guess)


{
$message = $_POST['guess'] ."is too big! Try a smaller
number";
}
elseif ($_POST['guess'] < $num_to_guess)
{
$message = $_POST['guess']. "is too small! Try a larger
number";
}
else
{ // must be equivalent
$message = "Well done!";
}
?>
<html>
<head>
<title> A PHP number guessing script</title>
</head>
<body>
<h1>
<?php print $message ?>
</h1>
<form action="<?php print $_SERVER['PHP_SELF']; ?>"
method="POST">
Type your guess here: <input type="text" name="guess">
<button type="submit" name="submit"
value="submit">submit</button>
</form>
</body>
</html>
OUTPUT
`

2) How to file uploads?


<html>
<body>
<form action="upload_file.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>

Upload_file.php

<?php
if($_FILES["file"]["error"]>0)
{
echo "error" .$_FILES["file"]["error"] ."<br>";
}
else
{
echo"upload:".$_FILES["file"]["name"] ."<br>";
echo "type=".$_FILES["file"]["type"]."<br>";
echo"size:".$_FILES["file"]["size"] ."<br>";
echo"stored in:".$_FILES["file"]["tmp_name"] ."<br>";
}
?>
OUTPUT

Web.txt
Hello Learners!
Welcome to PHP
3)File read the content
<?php
$file = "web.txt";
// Check the existence of file
if(file_exists($file))
{
// Open the file for reading
$handle = fopen($file, "r") or die("ERROR: Cannot open
the file.");
// Read fixed number of bytes from the file
$content = fread($handle, "200");

// Closing the file handle


fclose($handle);
// Display the file content
echo $content;
}
else
{
echo "ERROR: File does not exist.";
}
?>

4) To read the file content up to number of lines


Web2.txt
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
File2.php
<html>
<body>

<?php
$myfile = fopen("web2.txt", "r") or die("Unable to open
file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>

</body>
</html>
5) File copy to sub directory
Create upload directory then

Example.txt
i love india

filecopy.php
<?php
// Source file path
$file = "example.txt";
// Destination file path
$newfile = "duplicate.txt";
// Check the existence of file
if(file_exists($file))
{
// Attempt to copy file
if(copy($file, $newfile))
{
echo "File copied successfully.";
}
else
{
echo "ERROR: File could not be copied.";
}
}
else
{
echo "ERROR: File does not exist.";
}

OUTPUT

6)File name rename


<?php
$file = "example.txt";

// Check the existence of file


if(file_exists($file))
{
// Attempt to rename the file
if(rename($file, "newfile.txt"))
{
echo "File renamed successfully.";
}
else
{
echo "ERROR: File cannot be renamed.";
}
}
else
{
echo "ERROR: File does not exist.";
}
?>
OUTPUT

7) File deleted
<?php
unlink('duplicate.txt');
echo "File deleted successfully";
?>
8) Written into a file
<?php
$file = "note.txt";

// String of data to be written


$data = "hi i am a lecturer in neida";

// Open the file for writing


$handle = fopen($file, "w");

// Write data to the file


fwrite($handle, $data);

// Closing the file handle


fclose($handle);

echo "Data written to the file successfully.";


?>

9) How to create directory in php


<?php
// The directory path
$dir = "babai";
// Check the existence of directory
if(!file_exists($dir))
{
// Attempt to create directory
if(mkdir($dir))
{
echo "Directory created successfully.";
}
else
{
echo "ERROR: Directory could not be created.";
}
}
else
{
echo "ERROR: Directory already exists.";
}
?>
OUTPUT

You might also like