0% found this document useful (0 votes)
49 views10 pages

File Upload and Database Interaction

The document contains source code from multiple PHP scripts that perform file input/output operations and connect to a MySQL database. The scripts open, read from, and write to a sample text file. One script counts the number of lines and words in the file. Another connects to a MySQL database called "personal" and outputs the contents of a "details" table to an HTML table. A file upload form is also implemented to allow users to upload files to a specified directory on the server.

Uploaded by

Yukti Satheesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views10 pages

File Upload and Database Interaction

The document contains source code from multiple PHP scripts that perform file input/output operations and connect to a MySQL database. The scripts open, read from, and write to a sample text file. One script counts the number of lines and words in the file. Another connects to a MySQL database called "personal" and outputs the contents of a "details" table to an HTML table. A file upload form is also implemented to allow users to upload files to a specified directory on the server.

Uploaded by

Yukti Satheesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

19BCE1665

YUKTI S
LAB EXERCISE 11
SOURCE CODE:
<html>
<body>
<?php
if (file_exists('sample.txt'))
{
echo 'file found!';
$f=fopen('sample.txt','a');
}
else
{
echo 'The file does not exists<br>';
$f=fopen('sample.txt','a');
}
fclose($f);
?>
</body>
</html>

<html>
<body>
<?php
if (file_exists('sample.txt'))
{
echo 'file found!';
$f=fopen('sample.txt','a');
}
else
{
echo 'The file does not exists<br>';
$f=fopen('sample.txt','a');
}
fwrite($f,"\nHello! How are you?\n");
fclose($f);
?>
</body>
</html>

<html>
<body>
<?php

$f=fopen('sample.txt','r');
$count=0;
while(!feof($f)) {
echo fgets($f) . "<br>";
$count++;
}
echo "The number of lines in the sample.txt file is ".$count."<br>";
?>
</body>
</html>

<html>
<body>
<?php

$f=fopen('sample.txt','r');
$count=0;
while(!feof($f)) {
echo fgets($f) . "<br>";
$count++;
}
$wordCount = str_word_count(file_get_contents('sample.txt'));
echo "The number of words in sample.txt file is ".$wordCount."<br><br><br>";
?>
</body>
</html>

<html>
<body>
<?php
$f=fopen('sample.txt','r');
$unique = array_unique($my_array);
$dupes = array_diff_key( $my_array, $unique );
echo "<br><br><br>";
print_r(array_count_values($unique+$dupes));
?>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>19BCE1665</title>
</head>
<body>
<form action="ex5.php" method="post" enctype="multipart/form-data">
<h2>Upload File</h2>
<label for="fileSelect">Filename:</label>
<input type="file" name="photo" id="fileSelect">
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>
Php
<html>
<body>
<?php

if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
$allowed = array("txt" => "text/plain");
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];

// Verify file extension


$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file
format.");

$maxsize = 5 * 1024 * 1024;


if($filesize > $maxsize) die("Error: File size is larger than the allowed
limit.");

// Verify MYME type of the file


if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("D:/yukti" . $filename)){
echo $filename . " is already exists.";
} else{
move_uploaded_file($_FILES["photo"]["tmp_name"], "D:/Felci" .
$filename);
echo "Your file was uploaded successfully.";
}
} else{
echo "Error: There was a problem uploading your file. Please try again.";
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}
}
?>
</body>
</html>
OUTPUT:
SOURCE CODE:

<?php
$con=mysqli_connect("localhost","19BCE1665","[email protected]");
mysqli_select_db($con,"personal");
if(mysqli_connect_error())
{
die("Failed to connect with MySQL:". mysqli_connect_error());
}
$sq="select * from details";
$resultt=mysqli_query($con,$sq);
$roww=mysqli_fetch_array($resultt,MYSQLI_ASSOC);
$countt=mysqli_num_rows($resultt);
echo'<table border="2" cellpadding="1"
cellspacing="5"><tr><th>Name</th><td>'.$roww['Name'].'</td></tr
>';
echo'<tr><th>Age</th><td>'.$roww['Age'].'</td></tr>';
echo'<tr><th>Date of birth</th><td>'.$roww['DOB'].'</td></tr>';
echo'<tr><th>Phone No</th><td>'.$roww['Phone'].'</td></tr>';
echo'<tr><th>Email</th><td>'.$roww['Email
ID'].'</td></tr></table>';
?>

You might also like