Exp 8
Exp 8
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.
<?php
if (isset($_POST['createFile'])) {
$filename = $_POST['filename'];
$content = $_POST['content'];
54
Lab Manual – BESCES3203 –Web Development Lab Poornima University, Academic Year 2024-25
<?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>";
}
}
?>
<?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']);
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