0% found this document useful (0 votes)
3 views3 pages

Exp 8

The document is a lab manual for a web development course at Poornima University, detailing an experiment focused on file manipulation using PHP. It outlines the aim of creating a PHP web page that allows users to create, write, read, and upload files, providing sample code for implementation. Additionally, it includes a set of viva questions related to PHP file handling functions and security considerations.

Uploaded by

manishanchara4
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)
3 views3 pages

Exp 8

The document is a lab manual for a web development course at Poornima University, detailing an experiment focused on file manipulation using PHP. It outlines the aim of creating a PHP web page that allows users to create, write, read, and upload files, providing sample code for implementation. Additionally, it includes a set of viva questions related to PHP file handling functions and security considerations.

Uploaded by

manishanchara4
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/ 3

Lab Manual – BESCES3203 –Web Development Lab Poornima University, Academic Year 2024-25

Experiment No.:2

Name of Experiment: Develop a PHP web page to manipulating files such as creating,
writing, reading and uploading.

Aim: To design and develop a PHP-based web page that allows users to perform basic
file manipulation operations, including creating files, writing content to files, reading the
contents of files, and uploading files to the server.

CREATE A file_manipulation.php FILE


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Manipulation in PHP</title>
</head>
<body>
<h2>File Manipulation in PHP</h2>

<!-- Form to create and write to a file -->


<h3>Create and Write to a File</h3>
<form method="post">
<label for="filename">Filename:</label><br>
<input type="text" id="filename" name="filename" required><br><br>
<label for="content">Content:</label><br>
<textarea id="content" name="content" rows="4" cols="50"
required></textarea><br><br>
<input type="submit" name="createFile" value="Create & Write File">
</form>

<?php
if (isset($_POST['createFile'])) {
$filename = $_POST['filename'];
$content = $_POST['content'];
54
Lab Manual – BESCES3203 –Web Development Lab Poornima University, Academic Year 2024-25

// Create and write to the file


file_put_contents($filename, $content);
echo "<p>File '$filename' created and written successfully.</p>";
}
?>

<!-- Form to read a file -->


<h3>Read from a File</h3>
<form method="post">
<label for="readFilename">Filename to read:</label><br>
<input type="text" id="readFilename" name="readFilename"
required><br><br>
<input type="submit" name="readFile" value="Read File">
</form>

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

if (file_exists($readFilename)) {
$fileContent = file_get_contents($readFilename);
echo "<h4>Content of '$readFilename':</h4><pre>$fileContent</pre>";
} else {
echo "<p>File '$readFilename' does not exist.</p>";
}
}
?>

<!-- Form to upload a file -->


<h3>Upload a File</h3>
<form method="post" enctype="multipart/form-data">
<label for="uploadFile">Choose file:</label><br>
<input type="file" id="uploadFile" name="uploadFile" required><br><br>
<input type="submit" name="upload" value="Upload File">
</form>

<?php
if (isset($_POST['upload'])) {
55
Lab Manual – BESCES3203 –Web Development Lab Poornima University, Academic Year 2024-25

$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['uploadFile']['name']);

// Ensure the upload directory exists


if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0777, true);
}

if (move_uploaded_file($_FILES['uploadFile']['tmp_name'], $uploadFile)) {
echo "<p>File successfully uploaded.</p>";
echo "<p>Uploaded file path: <a href='$uploadFile'
target='_blank'>$uploadFile</a></p>";
} else {
echo "<p>File upload failed.</p>";
}
}
?>
</body>
</html>

VIVA QUESTION:

1. What PHP functions are used for creating and writing to a file?
2. How does the PHP file_get_contents() function work when reading a file?
3. What role does the $_FILES superglobal array play in file uploading?
4. What precautions should be taken when uploading files to prevent security
vulnerabilities?
5. How can you check if a file exists before performing operations on it in PHP?

56

You might also like